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.
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
> > 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”
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.
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: