Skip to content


Subversion checkout URL

You can clone with
Download ZIP


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

wants to merge 1 commit into from

4 participants


This avoids breaking from method chains in many cases. I don't see any other way to do this without this method.

Can someone confirm pull request are a valid way of submitting patches ? I cannot register on the mailing list for some reason, it seems to be broken.


[ 1, 2, 3, 4].select{|x| x.odd?}.embed{|x| {:total => x.count, :data => x}}

I'll add that I did not have any revelation on the method's name, i guess wrap would also be a nice candidate, happy to change that if needed.


It's OK you posted here.


  • Do you have any real-world use case? The example you provided is a bit too artificial that it seems less useful than {:total => [1, 2, 3, 4].select ... }.
  • Did you consider extending ActiveSupport? Other core people are puritanical to these nifty littile new methods these days. Perhaps it might be easier for you to first pursuade ActiveSupport guys, then let them pursuade us.

Hi shyouhei,

Thanks for the quick reply. Here's some clarifications:

  • The whole idea is to not break method chains, just like Object#tap so if you need more modifications it, e.g: [ 1, 2, 3, 4].select{|x| x.odd?}.embed{|x| {:total => x.count, :data => x}}.to_json
  • This is not specially targeted at rails code, but generic ruby programming (where you might not have ActiveSupport included). Otherwise, sure a gem could be used to re-open Object and add the method there.

I hope this helps a little


I think these other names could also be valid for this method:

  • wrap
  • fence
pyr commented

any other thoughts regarding this PR ?


Actually, Rails core have been considering introducing this method into ActiveSupport, but haven't come up with a good method name so far.

BTW I would like to see this feature implemented in Ruby, but -1 for the method names embed, wrap or fence.
IMO simply do would do since it simply does the block.


how about doto then, so it doesn't step on the do block word


Object#doto? That doesn't look like a typical English word...


the problem with do is that it would allow:

[].do do |obj|

I'm happy to see it considered for rails, but since I use ruby mostly outside of rails and like to keep my gem dependency graph small, I'd be very happy to see this go in without having to pull in activerecord


I think that with the recent addition of Enumerable#lazy this makes even more sense


Closing this, I've opened up a ticket on redmine as feature #7388

@zzak zzak closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 14, 2011
  1. @pyr

    * object.c: Add Object#embed which works like tap but returns the blo…

    pyr authored
    value. Useful in method chains.
This page is out of date. Refresh to see the latest.
Showing with 26 additions and 0 deletions.
  1. +5 −0 ChangeLog
  2. +21 −0 object.c
5 ChangeLog
@@ -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 <>
* test/ruby/test_io_m17n.rb
21 object.c
@@ -555,6 +555,26 @@ rb_obj_tap(VALUE 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}}
+ *
+ */
+rb_obj_embed(VALUE obj)
+ VALUE retval;
+ retval = rb_yield(obj);
+ return retval;
* Document-method: inherited
@@ -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, "is_a?", rb_obj_is_kind_of, 1);
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("format", rb_f_sprintf, -1); /* in sprintf.c */
Something went wrong with that request. Please try again.