Alias_method :tap, :affect

On Nov 12, 2007 1:10 PM, [email protected] wrote:

This is the first I’ve heard of Object#tap. It seems backward because
you almost always want the result of the block, not the object back
again. All of my ruby scripts use

class Object
def as
yield self
end
end

This can be useful in the right context. Should it be canonized in
Ruby? I’m not sure.

platform_audio_files = audio_stems.map { |f|
f + “.” + audio_ext[platform]
}.as { |t|
t + audio_basenames
}.map { |f|
File.join(audio_dir, f)
}

This method chaining looks ugly to me, but I’m not a CS guy; so, to
each his own.

If ruby is adding anything, it should be the behavior of Object#as,
not Object#tap.

You are suggesting that people add something that probably doesn’t
need to be added. I’m halfway convinced, but I think you should
present a couple more use cases for me to sway your way (sorry, folks,
for the poetic language :slight_smile:

Todd

From: furtive

On Nov 12, 2:31 pm, Martin

> #tap is the opposite use case - you want to “tap” the object stream,

> observing without affecting.

OK I understand Object#tap now. At first I thought the motivation was

to modify the object inside the block, but now I see that it can be a

useful part of functional-style ruby.

So, to throw this out again — there should also be Object#as which

returns the block result (I like the name ‘as’ but I am not

particularly attached to it). I can personally attest to its

usefulness, and I can show reams of examples in addition to the above

one I gave.

(my previous message seems to have lost, so i apologize for a 2nd post)

tap is indispensable for me when it comes to chaining (specially bang
methods)…

naive example follows…

~> s
=> “This is a test”

~> s.capitalize!.capitalize!
NoMethodError: undefined method `capitalize!’ for nil:NilClass

breaks!

~> s.upcase!.upcase!
=> nil

breaks!

~> s.upcase!.capitalize!
NoMethodError: undefined method `capitalize!’ for nil:NilClass

breaks!

~> s.tap(&:upcase!).tap(&:upcase!).tap(&:capitalize!).tap(&:capita
lize!).tap(&:downcase!).tap(&:downcase!)
=> “this is a test”

long chain, no break :slight_smile:

btw, i like the #tap name, it’s like tapping a chain so that it wont
break. (i’m not an english expert but that is how i usually use the
word)…

kind regards -botp

From: [email protected] [mailto:[email protected]]

On Nov 12, 2:31 pm, Martin DeMello [email protected] wrote:

> On Nov 12, 2007 11:10 AM, [email protected] wrote:

> > This is the first I’ve heard of Object#tap. It seems

backward because

> > you almost always want the result of the block, not the

object back

> > again.

> #tap is the opposite use case - you want to “tap” the object stream,

> observing without affecting.

OK I understand Object#tap now. At first I thought the motivation was

to modify the object inside the block, but now I see that it can be a

useful part of functional-style ruby.

~> s
=> “THIS IS A TEST”
~> s.upcase!.capitalize!
NoMethodError: undefined method `capitalize!’ for nil:NilClass

~> s.tap{|x| x.upcase!}.capitalize!
=> “This is a test”
~> s
=> “This is a test”

C:\ruby1.9\bin>ri Object#tap -T
------------------------------------------------------------- Object#tap
obj.tap{|x|…} => obj

 Returns the receiver after executing the block given. Its main
 purpose is to be inserted in the method chain.

kind regards -botp
The essence of knowledge, is having it, to apply it;…

Hi –

On Thu, 15 Nov 2007, Peña, Botp wrote:

usefulness, and I can show reams of examples in addition to the above

~> s.upcase!.capitalize!
btw, i like the #tap name, it’s like tapping a chain so that it wont break. (i’m not an english expert but that is how i usually use the word)…
I am an English expert and I have no idea what “tap” is supposed to
have to do with what this #tap method does.

David

On Nov 15, 2007, at 7:41 AM, David A. Black wrote:

I am an English expert and I have no idea what “tap” is supposed to
have to do with what this #tap method does.

It’s like “tapping” a call chain, just as you would tap a phone call.

James Edward G. II

Hi –

On Thu, 15 Nov 2007, James Edward G. II wrote:

On Nov 15, 2007, at 7:41 AM, David A. Black wrote:

I am an English expert and I have no idea what “tap” is supposed to
have to do with what this #tap method does.

It’s like “tapping” a call chain, just as you would tap a phone call.

It’s like tapping a phone call after the call is over :slight_smile:

David

From: David A. Black [mailto:[email protected]]

On Thu, 15 Nov 2007, James Edward G. II wrote:

> On Nov 15, 2007, at 7:41 AM, David A. Black wrote:

>> I am an English expert and I have no idea what “tap” is supposed to

>> have to do with what this #tap method does.

> It’s like “tapping” a call chain, just as you would tap a phone

call.

It’s like tapping a phone call after the call is over :slight_smile:

David

pardon me sir davide, but i am not sure what you mean there, in my
examples, i think tap tapped before it called upcase/capitalize, no?

kind regards -botp

Not sure that I am able to contribue to this discussion, but I must
admit neither .tap nor .affect are very intuitive for me on first glance
:slight_smile:

[email protected] wrote:

}

I greatly prefer the latter. I want to chain, chain, chain, and I
don’t want pesky prefix-y function calls like File.basename() to cramp
my style.

It wasn’t entirely a coincidence that JEGII found concat - it’s
the sort of thing that is often needed with arrays. The underlying
problem in your example is that filename operations aren’t object-
oriented, so you wind up using a method-call on a class… yuk.

This is a weakness in the standard classes - there should be a
Filename class, and a String#to_filename method, so your example
becomes:

filename = input.map { |t|
t.gsub(re, “_”)
}.join.to_filename.base

No need for Object#as in that. There are other use cases of course,
and I also might use “as” sometimes myself.

Clifford H…