Created
February 4, 2021 18:15
-
-
Save jbranchaud/3f7d77a94a234b4107b30d5b22122244 to your computer and use it in GitHub Desktop.
Returning out of a Ruby begin block
This file contains 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
class Thing | |
def initialize(value) | |
@value = value | |
end | |
def call(other_value) | |
@other_value = other_value | |
puts final_value | |
end | |
private | |
def final_value | |
@final_value ||= | |
begin | |
return if @other_value.nil? || @other_value == "" | |
if @value == @other_value | |
"SAME" | |
else | |
"DIFFERENT" | |
end | |
end | |
puts "Final Value set to #{@final_value}" | |
@final_value | |
end | |
end | |
puts "-- first --" | |
Thing.new("value").call("value") | |
puts "-- second --" | |
Thing.new("value").call("other value") | |
puts "-- third --" | |
Thing.new("value").call(nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment