def foo yield rescue puts "Only on error" else puts "Only on success" ensure puts "Always executed" end foo{ raise "Error" } puts "---" foo{ "No error" } # Output: # Only on error # Always executed # --- # Only on success # Always executed