Sha256: eb9b9a2359689d3501d74c73d422aeaa6f35deaff5aa85ddea19013e4c1330a6

Contents?: true

Size: 343 Bytes

Versions: 7

Compression:

Stored size: 343 Bytes

Contents

class Hash

  # A method to convert a Hash into a Struct.
  #
  #   h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
  #   s = h.to_struct("Foo")
  #
  # TODO: Is this robust enough considerd hashes aren't ordered?
  #
  #  CREDIT: Daniel Berger

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

end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
facets-2.4.0 lib/facets/hash/to_struct.rb
facets-2.4.1 lib/facets/hash/to_struct.rb
facets-2.4.2 lib/core/facets/hash/to_struct.rb
facets-2.4.3 lib/core/facets/hash/to_struct.rb
facets-2.4.4 lib/core/facets/hash/to_struct.rb
facets-2.4.5 lib/core/facets/hash/to_struct.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/hash/to_struct.rb