End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
OpenStruct#to_h
ostruct.to_h → hash
ostruct.to_h {|name, value| block } → hash
lib/ostruct.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/lib/ostruct.rb, line 169
def to_h(&block)
if block
@table.to_h(&block)
else
@table.dup
end
end
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.
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" }