本文簡要介紹ruby語言中 Marshal.load
的用法。
用法
load(source, proc = nil, freeze: false) → obj
也別名為:restore
返回將源中的序列化數據轉換為 Ruby 對象(可能帶有關聯的從屬對象)的結果。 source 可以是 IO
的實例或響應to_str 的對象。如果指定了 proc,則每個對象都將被傳遞給 proc,因為該對象正在被反序列化。
切勿將不受信任的數據(包括用戶提供的輸入)傳遞給此方法。請參閱概述了解更多詳情。
如果傳遞freeze: true
參數,反序列化的對象將被深度凍結。請注意,由於凍結的字符串重複數據刪除,它可能會導致更有效的內存使用:
serialized = Marshal.dump(['value1', 'value2', 'value1', 'value2'])
deserialized = Marshal.load(serialized)
deserialized.map(&:frozen?)
# => [false, false, false, false]
deserialized.map(&:object_id)
# => [1023900, 1023920, 1023940, 1023960] -- 4 different objects
deserialized = Marshal.load(serialized, freeze: true)
deserialized.map(&:frozen?)
# => [true, true, true, true]
deserialized.map(&:object_id)
# => [1039360, 1039380, 1039360, 1039380] -- only 2 different objects, object_ids repeating
相關用法
- Ruby Marshal.dump用法及代碼示例
- Ruby Marshal模塊用法及代碼示例
- Ruby Markdown類用法及代碼示例
- Ruby Markup類用法及代碼示例
- Ruby Markup.add_regexp_handling用法及代碼示例
- Ruby MatchData.pre_match用法及代碼示例
- Ruby Matrix lup()用法及代碼示例
- Ruby Matrix unitary?()用法及代碼示例
- Ruby Matrix symmetric?()用法及代碼示例
- Ruby Matrix t()用法及代碼示例
- Ruby Matrix identity()用法及代碼示例
- Ruby Matrix hash()用法及代碼示例
- Ruby Matrix hadamard_product()用法及代碼示例
- Ruby Matrix singular?()用法及代碼示例
- Ruby Math sqrt()用法及代碼示例
- Ruby Matrix round()用法及代碼示例
- Ruby Matrix collect()用法及代碼示例
- Ruby Math.acosh用法及代碼示例
- Ruby Matrix hermitian?()用法及代碼示例
- Ruby Matrix row_vectors()用法及代碼示例
- Ruby Matrix conjugate()用法及代碼示例
- Ruby Math.asinh用法及代碼示例
- Ruby Matrix cofactor()用法及代碼示例
- Ruby Matrix rectangular()用法及代碼示例
- Ruby Matrix component()用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Marshal.load。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。