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

struct.c: yield assoc in each_pair

* struct.c (rb_struct_each_pair): yield associated pairs so that
  an unsplat argument can get both, for consistency with Hash,
  OpenStruct, and etc.  [ruby-dev:46533] [Bug #7382]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37721 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-11-19 07:31:05 +00:00
parent 2370556d3d
commit 7588c674d5
3 changed files with 14 additions and 1 deletions

View file

@ -1,3 +1,9 @@
Mon Nov 19 16:30:59 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* struct.c (rb_struct_each_pair): yield associated pairs so that
an unsplat argument can get both, for consistency with Hash,
OpenStruct, and etc. [ruby-dev:46533] [Bug #7382]
Mon Nov 19 16:17:53 2012 Nobuyoshi Nakada <nobu@ruby-lang.org> Mon Nov 19 16:17:53 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* configure.in (LIBS): libelf is need on only FreeBSD. * configure.in (LIBS): libelf is need on only FreeBSD.

View file

@ -495,7 +495,9 @@ rb_struct_each_pair(VALUE s)
RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size); RETURN_SIZED_ENUMERATOR(s, 0, 0, rb_struct_size);
members = rb_struct_members(s); members = rb_struct_members(s);
for (i=0; i<RSTRUCT_LEN(s); i++) { for (i=0; i<RSTRUCT_LEN(s); i++) {
rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT_PTR(s)[i]); VALUE key = rb_ary_entry(members, i);
VALUE value = RSTRUCT_PTR(s)[i];
rb_yield(rb_assoc_new(key, vlaue));
} }
return s; return s;
} }

View file

@ -1,3 +1,4 @@
# -*- coding: us-ascii -*-
require 'test/unit' require 'test/unit'
require 'timeout' require 'timeout'
require_relative 'envutil' require_relative 'envutil'
@ -117,6 +118,10 @@ class TestStruct < Test::Unit::TestCase
klass = Struct.new(:a, :b) klass = Struct.new(:a, :b)
o = klass.new(1, 2) o = klass.new(1, 2)
assert_equal([[:a, 1], [:b, 2]], o.each_pair.to_a) assert_equal([[:a, 1], [:b, 2]], o.each_pair.to_a)
bug7382 = '[ruby-dev:46533]'
a = []
o.each_pair {|x| a << x}
assert_equal([[:a, 1], [:b, 2]], a, bug7382)
end end
def test_inspect def test_inspect