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

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

@pyr
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
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.

@shyouhei
Owner

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
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
pyr commented Dec 15, 2011

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

  • wrap
  • fence
@pyr
pyr commented Jan 3, 2012

any other thoughts regarding this PR ?

@amatsuda
Owner

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
pyr commented Jan 19, 2012

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

@amatsuda
Owner

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

@pyr
pyr commented Jan 19, 2012

the problem with do is that it would allow:

[].do do |obj|
   foo
end
@pyr
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
pyr commented Mar 23, 2012

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

@zzak
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
@wanabe wanabe added a commit to wanabe/ruby that referenced this pull request Feb 25, 2017
@mgaudet @wanabe mgaudet + wanabe Skip test_stackoverflow if JIT exists
To allow progress on passing test-all (Issue #67) until Issue #58 is
resolved.
0cc2e70
@wanabe wanabe added a commit to wanabe/ruby that referenced this pull request Feb 26, 2017
@mgaudet @wanabe mgaudet + wanabe Skip test_stackoverflow if JIT exists
To allow progress on passing test-all (Issue #67) until Issue #58 is
resolved.
c0c628c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment