Last active
August 29, 2015 14:02
-
-
Save dholdren/e6f3f09451a119febf61 to your computer and use it in GitHub Desktop.
Ruby Object#tap alternatives
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
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