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:
parent
9ad8d670ec
commit
8f6ce1a17e
2 changed files with 16 additions and 0 deletions
6
activesupport/lib/active_support/core_ext/struct.rb
Normal file
6
activesupport/lib/active_support/core_ext/struct.rb
Normal 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)
|
10
activesupport/test/core_ext/struct_test.rb
Normal file
10
activesupport/test/core_ext/struct_test.rb
Normal 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
|
Loading…
Reference in a new issue