Project

General

Profile

Feature #10095 ยป itself-block.patch

add optional block to #itself - phluid61 (Matthew Kerwin), 08/08/2014 02:12 AM

View differences:

object.c
404 404

  
405 405
/*
406 406
 *  call-seq:
407
 *     obj.itself -> an_object
407
 *     obj.itself               -> an_object
408
 *     obj.itself { |o| block } -> an_object
408 409
 *
409
 *  Returns <i>obj</i>.
410
 *  Returns <i>obj</i>.  If the optional <em>block</em> is supplied,
411
 *  the object is passed to it, and the block's result is returned.
410 412
 *
411 413
 *	string = 'my string' #=> "my string"
412 414
 *	string.itself.object_id == string.object_id #=> true
415
 *	string.itself { |s| s.upcase } #=> "MY STRING"
413 416
 *
414 417
 */
415 418

  
416 419
static VALUE
417 420
rb_obj_itself(VALUE obj)
418 421
{
422
	if (rb_block_given_p()) return rb_yield(obj);
419 423
    return obj;
420 424
}
421 425

  
test/ruby/test_object.rb
14 14

  
15 15
  def test_itself
16 16
    feature6373 = '[ruby-core:44704] [Feature #6373]'
17
    feature10095 = '[ruby-core:64039] [Feature #10095]'
17 18
    object = Object.new
18 19
    assert_same(object, object.itself, feature6373)
20
    assert_same(object, object.itself{|o| o}, feature10095)
21

  
22
    string = 'foo'
23
    assert_equal('FOO', string.itself {|o| o.upcase}, feature10095)
24
    assert_equal('foo', string, feature10095)
19 25
  end
20 26

  
21 27
  def test_dup