Permalink
Browse files

backporting Struct#to_h from ruby 2.0

  • Loading branch information...
1 parent 9ad8d67 commit 8f6ce1a17ea1d73c3becc16ece79046e113daf8e @tenderlove tenderlove committed Nov 13, 2012
Showing with 16 additions and 0 deletions.
  1. +6 −0 activesupport/lib/active_support/core_ext/struct.rb
  2. +10 −0 activesupport/test/core_ext/struct_test.rb
@@ -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)
@@ -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

0 comments on commit 8f6ce1a

Please sign in to comment.