Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* Add Object#embed which works like tap but returns the block's value #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,8 @@
Wed Dec 14 22:23:14 2011 <pyr@omega>

* object.c: Add Object#embed which works like tap but returns the block's
value. Useful in method chains.

Wed Dec 14 21:58:42 2011 NAKAMURA Usaku <usa@ruby-lang.org> Wed Dec 14 21:58:42 2011 NAKAMURA Usaku <usa@ruby-lang.org>


* test/ruby/test_io_m17n.rb * test/ruby/test_io_m17n.rb
Expand Down
21 changes: 21 additions & 0 deletions object.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -555,6 +555,26 @@ rb_obj_tap(VALUE obj)
return obj; return obj;
} }


/*
* call-seq:
* obj.embed{|x|...} -> obj
*
* Yields <code>x</code> to the block, and then returns the return value
* of the block. The primary purpose of this method is to modify the object
* in place in a method chain.
*
* (1..10).embed {|x| {:numbers => x}}
*
*/
VALUE
rb_obj_embed(VALUE obj)
{
VALUE retval;

retval = rb_yield(obj);
return retval;
}



/* /*
* Document-method: inherited * Document-method: inherited
Expand Down Expand Up @@ -2780,6 +2800,7 @@ Init_Object(void)
rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1); rb_define_method(rb_mKernel, "kind_of?", rb_obj_is_kind_of, 1);
rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1); rb_define_method(rb_mKernel, "is_a?", rb_obj_is_kind_of, 1);
rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0); rb_define_method(rb_mKernel, "tap", rb_obj_tap, 0);
rb_define_method(rb_mKernel, "embed", rb_obj_embed, 0);


rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */ rb_define_global_function("sprintf", rb_f_sprintf, -1); /* in sprintf.c */
rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */ rb_define_global_function("format", rb_f_sprintf, -1); /* in sprintf.c */
Expand Down