Search code examples
rubyvariablessyntaxraisehashrocket

What's the syntax (=>) used in assign error object to variable of `rescue` method?


The rescue which could assigns a variable to reference the error object has this syntax (=>)

rescue => e

If rescue is the one of the general method call, what's the meaning of =>. Could I use the same syntax on other method call?

my_method arg1, arg2 => my_obj

Solution

  • While raise is indeed a method, rescue is not. It is a keyword and defined on parse.y:10467. As such, the syntax you have is special to rescue (since => e isn't any sort of method argument), and not valid for methods themselves (at least not with the same meaning). How/where the rescue => e syntax itself is defined in the parser I'm not entirely sure.