Skip to content

Instantly share code, notes, and snippets.

@leandroh
Created February 18, 2010 05:53
Show Gist options
  • Save leandroh/307394 to your computer and use it in GitHub Desktop.
Save leandroh/307394 to your computer and use it in GitHub Desktop.
module Varz
module GetSet
def initialize(init = {})
@variables = init
end
def get(key)
@variables[key] || "Bang!"
end
def set(key, value = nil)
@variables[key] = value if value
end
def to_a
@variables.inject([]) do |arr, v|
arr << "#{v.first} => #{v.last}"
end
end
def to_s
to_a.join(", ")
end
end
end
module Varz
class Store
include GetSet
def [](key)
get(key)
end
def []=(key, value=nil)
set(key, value)
end
end
end
vars = {
:project => "Varz",
:version => "0.0.2",
:author => "prz"
}
tree = Varz::Store.new(vars)
tree.set("xfile", "X-File")
tree.set("xfilex")
p tree.get("xfilex")
p tree.get("xfile")
p tree.get(:project)
tree.set(:value, "waaah!!!")
p tree.get(:value)
p tree.get(:project)
p tree.to_a
p tree.to_s
tree[:project] = "Varz::Base"
p tree[:project]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment