Skip to content

Instantly share code, notes, and snippets.

@davejachimiak
Last active December 24, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davejachimiak/6734187 to your computer and use it in GitHub Desktop.
Save davejachimiak/6734187 to your computer and use it in GitHub Desktop.
FromHashable
module FromHashable
def initialize hash
@hash = hash
set_ivars
set_methods
set_as_hash
end
private
def set_ivars
@hash.each do |k, v|
instance_variable_set :"@#{k}", v
end
end
def set_methods
@hash.each { |k, v| define_singleton_method k.to_sym, ->{ v } }
end
def set_as_hash
define_singleton_method :as_hash, ->{ @hash }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment