From: dblack@... Date: 2006-11-17T20:57:58+09:00 Subject: Re: #returning and #tap Hi -- On Fri, 17 Nov 2006, Martin DeMello wrote: > On 11/17/06, dblack@wobblini.net wrote: >> >> I wonder if there's some name that embodies the fact that the object >> is going to come back from this method call. The temporal ones (then, >> and_then, etc.) sound kind of self-evident ("Well, of course the next >> thing happens 'then'," my brain says). > > Good point! I thought of "pipe" but even that doesn't say that it > modifies and returns self. Maybe "apply": string.split(/\s/).apply > {|i| i.pop}.whatever. Or even "modify". > >> Perhaps that was the reasoning >> behind "tap", though "tap" doesn't communicate much to me. I'm not >> sure. > > 'tap' had a different intent, though - "tee off the object without > disturbing it" - even if it did the same thing in the end. So you > would typically take a.foo.bar.baz.quux... and drop in a tap, > a.foo.bar.tap {|i| puts "hi mom! this is #{i}"}.baz.quux, and take > care not to modify i destructively. Although... there might be cases where disturbing the object would be desireable. For example, you could use it to work around the fact that a lot of bang methods return nil when there's no change: str = "abcde" a = str.tap {|s| s.gsub!(/z/,"x") }.split(//) David -- David A. Black | dblack@rubypal.com Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org