Sha256: 5f426464abda053b95be648f4db9f51dafaec934bc1f6df378c28684d04123b3

Contents?: true

Size: 601 Bytes

Versions: 15

Compression:

Stored size: 601 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

15 entries across 15 versions & 1 rubygems

Version Path
facets-1.4.1 lib/facets/core/hash/to_ostruct.rb
facets-1.4.2 lib/facets/core/hash/to_ostruct.rb
facets-1.4.3 lib/facets/core/hash/to_ostruct.rb
facets-1.4.4 lib/facets/core/hash/to_ostruct.rb
facets-1.4.5 lib/facets/core/hash/to_ostruct.rb
facets-1.7.0 lib/facets/core/hash/to_ostruct.rb
facets-1.7.30 lib/facets/core/hash/to_ostruct.rb
facets-1.7.38 lib/facets/core/hash/to_ostruct.rb
facets-1.7.46 lib/facets/core/hash/to_ostruct.rb
facets-1.8.0 lib/facets/core/hash/to_ostruct.rb
facets-1.8.20 lib/facets/core/hash/to_ostruct.rb
facets-1.8.51 lib/facets/core/hash/to_ostruct.rb
facets-1.8.49 lib/facets/core/hash/to_ostruct.rb
facets-1.8.8 lib/facets/core/hash/to_ostruct.rb
facets-1.8.54 lib/facets/core/hash/to_ostruct.rb