Created
October 14, 2014 04:04
-
-
Save sai43/65737859fd57623341d5 to your computer and use it in GitHub Desktop.
singleton example in ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'singleton' | |
class Example | |
include Singleton | |
attr_accessor :keep, :strip | |
def _dump(depth) | |
# this strips the @strip information from the instance | |
Marshal.dump(@keep, depth) | |
end | |
def self._load(str) | |
instance.keep = Marshal.load(str) | |
instance | |
end | |
end | |
a = Example.instance | |
a.keep = "keep this" | |
a.strip = "get rid of this" | |
a.taint | |
stored_state = Marshal.dump(a) | |
a.keep = nil | |
a.strip = nil | |
b = Marshal.load(stored_state) | |
p a == b # => true | |
p a.keep # => "keep this" | |
p a.strip # => nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment