Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Module
- # Take the name of a variable containing a hash and a list of keys
- # and define getter methods for those hash items
- # Keys need to be symbols or strings
- def hash_reader(hashvar, *keys)
- keys.each do |key|
- define_method(key) do
- hash = instance_variable_get hashvar
- hash[key]
- end
- end
- end
- end
- class Foo
- hash_reader :@foo, :foo, :bar, :baz
- def initialize(foo)
- @foo=foo
- end
- end
- Foo.new(:foo => "bar", :bar => 42, :baz => 23).bar
- #=> 42
Add Comment
Please, Sign In to add comment