4 ms·
"try" is an active support extension on "nil", so if you're using rails (or active support), then you already have this functionality. The point being made here
by maniacalrobot 11y ago
"try" is an active support extension on "nil", so if you're using rails (or active support), then you already have this functionality. The point being made here is that patterns developed outside the core Ruby development process are getting adopted upstream by the language for all Ruby applications to benefit from.
Also, there your example or using try falls short if the methods require additional arguments to be passed in. "try" already handles this by requiring you to "try" each method.
Of cause there is a very good argument to be had that this pattern also breaks the law of demeter by allowing you to call methods on objects that you don't own a stake in.
- hiharryhere 11y agoFYI .try takes a block. Passing a symbol is just shorthand, similar to how map(&:method) is shorthand for .map {|x| x.method } So technically you can pass arguments to a try method by doing object.try {|o| o.method(args) } Not trying to be pedantic, hopefully being helpful.
- maniacalrobot 11y agoEveryday is a learning day!