diff --git a/object.c b/object.c index b994539..78f4ac3 100644 --- a/object.c +++ b/object.c @@ -404,18 +404,22 @@ rb_obj_dup(VALUE obj) /* * call-seq: - * obj.itself -> an_object + * obj.itself -> an_object + * obj.itself { |o| block } -> an_object * - * Returns obj. + * Returns obj. If the optional block is supplied, + * the object is passed to it, and the block's result is returned. * * string = 'my string' #=> "my string" * string.itself.object_id == string.object_id #=> true + * string.itself { |s| s.upcase } #=> "MY STRING" * */ static VALUE rb_obj_itself(VALUE obj) { + if (rb_block_given_p()) return rb_yield(obj); return obj; } diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index 4573dc8..67b98d1 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -14,8 +14,14 @@ class TestObject < Test::Unit::TestCase def test_itself feature6373 = '[ruby-core:44704] [Feature #6373]' + feature10095 = '[ruby-core:64039] [Feature #10095]' object = Object.new assert_same(object, object.itself, feature6373) + assert_same(object, object.itself{|o| o}, feature10095) + + string = 'foo' + assert_equal('FOO', string.itself {|o| o.upcase}, feature10095) + assert_equal('foo', string, feature10095) end def test_dup