Turning Things Into Hashes
> {}.to_h
=> {}
> {hello: "world"}.to_h
=> {:hello=>"world"}> nil.to_h
=> {}> [:one, 2].to_h
TypeError: wrong element type Symbol at 0 (expected array)
from (pry):36:in `to_h'> [[:one, 2], [:three, 4]].to_h
=> {:one=>2, :three=>4}> Person = Struct.new(:name, :age)
=> Person
> bob = Person.new("bob", 45)
=> #<struct Person name="bob", age=45>
> bob.to_h
=> {:name=>"bob", :age=>45}Last updated