I have a ruby hash, say
h={name: "john", age: "23"}
It is not an object, just a hash created from an object. I want to access its values with the method attribute as I access an object. i.e.:
h.name => "john"
h.age => 23
Is it possible to do this?
In your case it will be handy to use openstruct
require 'ostruct'
h = OpenStruct.new(name: "john", age: "23")
h.name #=> "john"
h.age #=> 23
Maybe this is what you are looking for
item = Struct.new(:id, :name)
item.new(1, 'Name')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With