Life In The Stack Trace...

TODO: <Insert clever marketing phrase here>

Ruby 2.0 NilClass OpenStruct Struct have a to_h method

Some Ruby 2.0 classes have a toh method. toh converts the object to a Hash. At the time of this writing, the following classes have the to_h method:

  • ENV
  • NilClass
  • OpenStruct
  • Struct

Here is the contents of test.rb

require 'ostruct'

Book = Struct.new(:author, :title, :description)
p Book.new("Duck", "Ruby 2.0", "A short description").to_h
#=> {:author=>"Duck", :title=>"Ruby 2.0", :description=>"A short description"}

truck = OpenStruct.new
truck.make = "Jeep"
truck.model = "Grand Cherokee Laredo"
p truck.to_h #=> {:make=>"Jeep", :model=>"Grand Cherokee Laredo"}

p nil.to_h   #=> {}

# uncomment and you will see a Hash of your environment
#p ENV.to_h