Sha256: ba87e1d0015a0a9509afcac537386485fec4a889ab0638ef480203b582c72f00

Contents?: true

Size: 603 Bytes

Versions: 11

Compression:

Stored size: 603 Bytes

Contents

require 'ostruct'

class Hash

  # Turns a hash into a generic object using an OpenStruct.
  #
  #   o = { 'a' => 1 }.to_ostruct
  #   o.a  #=> 1
  #
  def to_ostruct
    ::OpenStruct.new( self )
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCHash < Test::Unit::TestCase

    def test_to_ostruct
      a = { :a => 1, :b => 2, :c => 3 }
      ao = a.to_ostruct
      assert_equal( a[:a], ao.a )
      assert_equal( a[:b], ao.b )
      assert_equal( a[:c], ao.c )
    end

  end

=end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
facets-0.9.0 lib/nano/hash/to_ostruct.rb
facets-1.0.0 lib/facet/hash/to_ostruct.rb
facets-1.0.3 packages/core/lib/facet/hash/to_ostruct.rb
facets-1.1.0 lib/facet/hash/to_ostruct.rb
facets-1.2.0 lib/facets/core/hash/to_ostruct.rb
facets-1.2.1 lib/facets/core/hash/to_ostruct.rb
facets-1.3.0 lib/facets/core/hash/to_ostruct.rb
facets-1.3.1 lib/facets/core/hash/to_ostruct.rb
facets-1.3.2 lib/facets/core/hash/to_ostruct.rb
facets-1.3.3 lib/facets/core/hash/to_ostruct.rb
facets-1.4.0 lib/facets/core/hash/to_ostruct.rb