Return to Snippet

Revision: 8874
at October 11, 2008 18:38 by pmd


Initial Code
# Nested hash to nested struct.

# From: http://www.roscripts.com/snippets/show/20
# Author: Mike Hasting (duvion)

# Note: An empty string used as a key will cause 1.8 to choke

class Hash
   def to_struct(struct_name)
      Struct.new(struct_name,*keys).new(*values)
   end
end

if $0 == __FILE__
   h = {"name"=>"Dan","age"=>33,"address"=>{"street"=>"Burlington rd, 33"}}

   h["address"]=h["address"].to_struct("B")
   s = h.to_struct("Foo")

   puts s.inspect
   puts "name: " + s.name
   puts "age: " + s.age.to_s
   puts "street: " + s.address.street
end

Initial URL
http://www.roscripts.com/snippets/show/20

Initial Description


Initial Title
Nested hash to nested struct

Initial Tags


Initial Language
Ruby