0

I would like to implement something with a rescue-like syntax.

begin raise "Failed" rescue Exception => e puts e end 

This works, and e is assigned to the corresponding value. But used in a method, this will raise an exception saying that e is undefined. In other words, how can rescue assigns something to e this way without throwing an undefined error?

class MyClass def to_s "hello" end end def my_method puts e.to_s end my_method(MyClass => e) #=> undefined local variable or method `e' for main:Object 
3
  • Why wouldn't you just pass MyClass as a parameter? Commented Mar 12, 2012 at 14:44
  • In fact, I just want to know how rescue works... :-) Commented Mar 12, 2012 at 14:51
  • It's a syntactic construct, part of the lex/parse process. You'd have to change the language. Commented Mar 12, 2012 at 14:55

1 Answer 1

1

Perhaps what you are looking for is:

class MyClass def self.hello puts "This is a class method." end def bye puts "This is an instance method." end end def my_method(params) klass = params[:class] puts klass.hello # Call a class method inst = klass.new # Create an instance puts inst.bye # Call an instance method end my_method(:class => MyClass) 

Three things to note:

  • Although the rescue syntax and the "named parameter" syntax look the same, all they have in common is the => operator. In the first case, you are telling to rescue the Exception "into" the variable e, effectively storing it in that variable. In the second case, you are telling Ruby to collect all parameters passed to the method and store them in a hash, using the supplied key/value pairs. Effectively, you are storing MyClass in the params hash, under the key :class.
  • In your above example, the to_s definition will not be callable on MyClass itself, because you defined it as an instance method. Instance methods are only available when you create an "instance" of the class with inst = MyClass.new. Then, you can call inst.to_s. Think of the class as an abstract "type" of thing, and of the instance as a concrete thing of that type. If you want the method to be available on the class, not the instances, you need to prefix it with self. I have illustrated the two different syntaxes above.
  • Again in your example, you are using def MyClass, which Ruby will interpret as "define a method with the name MyClass". If you want to define a class, you need to use class MyClass instead.

Hope this clarifies things a bit.

Sign up to request clarification or add additional context in comments.

2 Comments

Based on what you said, i.e. "In the first case, you are telling to rescue the Exception "into" the variable e, effectively storing it in that variable.", is it be possible to reproduce that exact same feature?
As others have commented, no, this is not possible. That's what method parameters - regular or "named" - are for! Having a method rely on parameters that aren't defined inside the method definition is asking for trouble. I guess that's why I don't know of any language that supports it :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.