Ruby Question

Burdette Lamar - Jul 1 '19 - - Dev Community

The documentation for method :to_h in Ruby class OpenStruct says this:

Converts the OpenStruct to a hash with keys representing each attribute (as symbols) and their corresponding values.

If a block is given, the results of the block on each pair of the receiver will be used as pairs.

And it offers this example:

require "ostruct"
data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
data.to_h   # => {:country => "Australia", :capital => "Canberra" }
data.to_h {|name, value| [name.to_s, value.upcase] }
            # => {"country" => "AUSTRALIA", "capital" => "CANBERRA" }

When I do the same in irb (version: irb 0.9.6(09/06/30)), I get this:

require "ostruct"
# => true
data = OpenStruct.new("country" => "Australia", :capital => "Canberra")
# => #<OpenStruct country="Australia", capital="Canberra">
data.to_h
# => {:country=>"Australia", :capital=>"Canberra"}
data.to_h {|name, value| [name.to_s, value.upcase] }
# => {:country=>"Australia", :capital=>"Canberra"}

The difference:

  • In the documentation, the last two values are different.
  • In the irb session, they are the same.

So, my question: Is this a documentation error?

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player