I have this code:
def with_else
puts 'we enter something funny'
if true
puts "yes"
'return YES'
end
'return what?'
else
puts 'no'
'return else -> no'
end
puts with_else
and the output is this:
we enter something funny
yes
no
return else -> no
Why don't I get an error for that? What does def
/else
/end
mean?
http://rubyfiddle.com/riddles/8df07
This is actually part of a larger syntactical structure:
def foo
# method stuff
rescue
# stuff if an exception is caught
else
# stuff if no exception is caught
ensure
# always run after rescue and else
end
But each section is optional! So you can leave out the rescue
and ensure
to get your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With