1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

backporting Struct#to_h from ruby 2.0

This commit is contained in:
Aaron Patterson 2012-11-13 11:11:11 -08:00
parent 9ad8d670ec
commit 8f6ce1a17e
2 changed files with 16 additions and 0 deletions

View file

@ -0,0 +1,6 @@
# Backport of Struct#to_h from Ruby 2.0
class Struct # :nodoc:
def to_h
Hash[members.zip(values)]
end
end unless Struct.instance_methods.include?(:to_h)

View file

@ -0,0 +1,10 @@
require 'abstract_unit'
require 'active_support/core_ext/struct'
class StructExt < ActiveSupport::TestCase
def test_to_h
x = Struct.new(:foo, :bar)
z = x.new(1, 2)
assert_equal({ foo: 1, bar: 2 }, z.to_h)
end
end