Created
June 11, 2012 22:51
-
-
Save pjb3/2913229 to your computer and use it in GitHub Desktop.
Tap that object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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 |
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!)
endWhich 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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To be fair, most use Object#tap when there's more than one thing to be done with the object.