Skip to content

Instantly share code, notes, and snippets.

@dholdren
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dholdren/e6f3f09451a119febf61 to your computer and use it in GitHub Desktop.
Save dholdren/e6f3f09451a119febf61 to your computer and use it in GitHub Desktop.
Ruby Object#tap alternatives
def foo
puts "something"
end
def bar
puts "something else"
end
#current
def pull
transaction_successful?.tap do |success|
begin
foo
bar
end if success
end
end
def pulla
if transaction_successful?
foo
bar
true
end
end
def pullb
begin
foo
bar
end if success = transaction_successful?
success
end
def pullc
if success = transaction_successful?
foo
bar
end
success
end
#false case
def transaction_successful?; false; end # !> previous definition of transaction_successful? was here
pull # => false
pulla # => nil
pullb # => false
pullc # => false
#true case
def transaction_successful?; true; end # !> method redefined; discarding old transaction_successful?
pull # => true
pulla # => true
pullb # => true
pullc # => true
# >> something
# >> something else
# >> something
# >> something else
# >> something
# >> something else
# >> something
# >> something else
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment