Sha256: e1b2bca55d36f211d07583f925d631c76a8944bf2dd9aca2b6711ab8443be81e
Contents?: true
Size: 715 Bytes
Versions: 6
Compression:
Stored size: 715 Bytes
Contents
# Convert hash to object with methods # o = FreeStruct.new name: 'a' # o.name -> 'a' # o[:name] -> 'a' # o.name = 'b' # o.name 'b' # o.name -> 'b' # o.name = nil # o.name -> nil # o.title -> raises error class FreeStruct def initialize hash @keys = hash.keys hash.each do |key, value| ivar = "@#{key}" instance_variable_set ivar, value define_singleton_method(key) do instance_variable_get ivar end define_singleton_method "#{key}=" do |val| instance_variable_set ivar, val end end end def [] key send key end def to_h @keys.inject({}) do |out, key| out[key] = send(key) out end end end
Version data entries
6 entries across 6 versions & 1 rubygems