Created
November 19, 2010 22:04
-
-
Save dscataglini/707277 to your computer and use it in GitHub Desktop.
It should execute the block only if the passed in value is true, and return the conditional. Caller can optionally pass a value to be yield
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 yield_if?(bool = false, value = nil) | |
yield(value) if bool && block_given? | |
bool | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Too many times I end up writing code that looks like this
or
def foo
if ret = some_expensive_call
do_something
end
ret
end
What I want is:
I could do this:
Although I am not sure about the name for it. I also wonder if it already exists.
It calls the block only if the conditional is true, but I want the result of the conditional.