-
-
Save pjb3/2913229 to your computer and use it in GitHub Desktop.
# To tap | |
def activate_person(person_id) | |
Person.find(person_id).tap do |person| | |
person.activate! | |
end | |
end | |
# or not to tap | |
def activate_person(person_id) | |
person = Person.find(person_id) | |
person.activate! | |
person | |
end | |
# that is the question |
To be fair, most use Object#tap when there's more than one thing to be done with the object.
Same thing though, then it is:
Person.find(person_id) do |person|
person.this
person.that
person.the_other_thing
end
vs.
person = Person.find(person_id)
person.this
person.that
person.the_other_thing
person
First one can be:
def activate_person(person_id)
Person.find(person_id).tap(&:activate!)
end
Which looks a bit more pretty to me.
@franciscoj yeah, that I could actually get behind.
@pjb3 do you have any particular reason to prefer the no-tap version? In what way is the tap version worse, in your view?
(I actually like tap, but I don't have any real rationale for why … I'd love to see a reasoned argument for one way or the other.)
@glv @pjb3 A few months back @brixen saw me using #tap
and lamented on how it's not ideal because it creates a new scope when a perfectly suitable one already exists. From what I remember he said it's not worth extending the stack for no good reason. Why make the VM do more work when the context is already set up already? @brixen, did I get that right?
Object#tap makes me 😡.
Triple 😡 if you call it the K-Combinator.