ç¹ç°ã¡ã½ããã追å ããHashã®ã¤ã³ã¹ã¿ã³ã¹ãMarshalã§dumpï¼loadãããã¨ãã¦ãããï¼ãã¾ããããªãã¦ï¼å°ã£ã¦ãï¼
Marshal.dumpã§Hashã®ã¤ã³ã¹ã¿ã³ã¹ãdumpã§ããï¼
def set_foo(obj) class << obj def foo "foo" end end obj end p Marshal.dump({ :a => 'aaaaa'}) #=> "\004\b{\006:\006a\"\naaaaa"
ç¹ç°ã¡ã½ãããå®ç¾©ãã¦ã¿ãï¼
def set_foo(obj) class << obj def foo "foo" end end obj end puts set_foo({ :a => 'aaaaa'}).foo # => foo
ã§ããï¼
ç¹ç°ã¡ã½ãããå®ç¾©ãããªãã¸ã§ã¯ãã¯dumpã§ããªãã®ã§ï¼ãããdumpãããã¨ããã¨ï¼ä¾å¤ãèµ·ããï¼
def set_foo(obj) class << obj def foo "foo" end end obj end p Marshal.dump(set_foo({ :a => 'aaaaa'})) #=> `dump': singleton can't be dumped (TypeError)
ç¹ç°ã¡ã½ããã§ï¼marshal_dumpãå®ç¾©ããã¨dumpã§ããããã«ãªããï¼ãªã«ãããã«dumpããå ´åã¨ç°ãªãçµæã«ãªã£ã¦ãã¾ãï¼
def set_foo(obj) class << obj def foo "foo" end def marshal_dump dup end end obj end p Marshal.dump(set_foo({ :a => 'aaaaa'})) #=> "\004\bU:\tHash{\006:\006a\"\naaaaa"
å é ã«ï¼:\tHash ã¨ããæååãå ãã£ã¦ãï¼
ãã®dumpããã®ãMarshal.loadãããã¨ããã¨ï¼Hashã¯ã©ã¹ã®marshal_loadãå¼ã¼ãã¨ãããï¼å®ç¾©ãã¦ãªãã®ã§ï¼åããªãï¼
def set_foo(obj) class << obj def foo "foo" end def marshal_dump dup end end obj end p Marshal.load(Marshal.dump(set_foo({ :a => 'aaaaa'}))) #=> instance of Hash needs to have method `marshal_load' (TypeError)
Hashã¯ã©ã¹ã«marshal_loadãå®ç¾©ããã¨ï¼loadã§ããããã«ãªããï¼ãããªã¨ãããæ¸ãæãã¦ãã¨ï¼æãã¬ã¨ããã§å¯ä½ç¨ãããããï¼
class Hash def marshal_load(obj) self.update(obj) end end def set_foo(obj) class << obj def foo "foo" end def marshal_dump dup end end obj end p Marshal.load(Marshal.dump(set_foo({ :a => 'aaaaa'}))) #=> {:a=>"aaaaa"}
gem memcache-clientãmemcachedã«ãªãã¸ã§ã¯ããä¿åããã¨ãã«ï¼Marshal.dumpã§ã·ãªã¢ã©ã¤ãºãã¦ãã¦ï¼ãã®ããã«ç¹ç°ã¡ã½ãããå®ç¾©ãã¦ãããªãã¸ã§ã¯ãããã£ãã·ã¥ãããã¨ãã§ããªãï¼å°ã£ãï¼