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

Closed
wants to merge 1 commit into
from

Conversation

4 participants

pyr commented Dec 14, 2011

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.

Example:

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

pyr commented Dec 14, 2011

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.

Owner

shyouhei commented Dec 14, 2011

It's OK you posted here.

Thoughts:

  • 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.

pyr commented Dec 14, 2011

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

pyr commented Dec 15, 2011

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

  • wrap
  • fence

pyr commented Jan 3, 2012

any other thoughts regarding this PR ?

Owner

amatsuda commented Jan 19, 2012

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.

pyr commented Jan 19, 2012

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

Owner

amatsuda commented Jan 19, 2012

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

pyr commented Jan 19, 2012

the problem with do is that it would allow:

[].do do |obj|
   foo
end

pyr commented Jan 19, 2012

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

pyr commented Mar 23, 2012

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

Member

zzak commented Nov 18, 2012

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

@zzak zzak closed this Nov 18, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment