Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created June 11, 2012 22:51
Show Gist options
  • Save pjb3/2913229 to your computer and use it in GitHub Desktop.
Save pjb3/2913229 to your computer and use it in GitHub Desktop.
Tap that object
# 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
@pjb3
Copy link
Author

pjb3 commented Jun 11, 2012

Object#tap makes me 😡.

Triple 😡 if you call it the K-Combinator.

@luigi
Copy link

luigi commented Jun 11, 2012

To be fair, most use Object#tap when there's more than one thing to be done with the object.

@pjb3
Copy link
Author

pjb3 commented Jun 11, 2012

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

@franciscoj
Copy link

First one can be:

def activate_person(person_id)
  Person.find(person_id).tap(&:activate!)
end

Which looks a bit more pretty to me.

@pjb3
Copy link
Author

pjb3 commented Jun 11, 2012

@franciscoj yeah, that I could actually get behind.

@glv
Copy link

glv commented Jun 12, 2012

@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.)

@jc00ke
Copy link

jc00ke commented Jun 12, 2012

@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