ruby delegate if object responds to method
I’d like to use delegation in Ruby, but I can’t guarantee that the target object responds to all method I will call. May I define delegation with a default behaviour?
Now, the above example would raise:
NoMethodError:
undefined method `age' for #<Student:0xa121212>
I have found the easiest way to delegate methods to the original object is with SimpleDelegator (ships in stdlib)
From the docs:
SimpleDelegator class provides the means to delegate all supported method calls to the object passed into the constructor