Jeffrey Yong Jeffrey Yong - 3 months ago 31
Ruby Question

=> Operator in Ruby

I have been searching thoroughly on Google but I can't seem to be able to find the answer. I am wondering what the

operator means in the Ruby code shown below:

def test_raising_a_particular_error
result = nil
# 'raise' and 'fail' are synonyms
raise MySpecialError, "My Message"
rescue MySpecialError => ex
result = :exception_handled

assert_equal :exception_handled, result
assert_equal "My Message", ex.message


There is no => operator in Ruby. There are, however, two uses of => in Ruby:

  1. in a Hash literal between the key object and the value object
  2. in a rescue clause after the exception filter expression, naming the variable the exception should be bound to

So, in this case, it's #2, and means "rescue any exception that is an instance of MySpecialError and bind it to the local variable named ex".