Getting access to Object#try
Asked Answered
A

1

6

I am trying to obtain access to Object#try in ruby.

I thought that just including activesupport would do the trick, but it doesn't.

irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support'
=> true
irb(main):003:0> o = Object.new
=> #<Object:0x15d45d9>
irb(main):004:0> o.respond_to? :try
=> false
irb(main):005:0> 

How do I get access to Object#try?

Agraffe answered 31/3, 2011 at 22:19 Comment(0)
N
4

To get try all you need to do is require "active_support/core_ext/object/try". The source is here and because the NilClass is opened and try is defined to return nil, you get the chaining ability.

Northeasterly answered 31/3, 2011 at 23:10 Comment(1)
@Christopher Maujean Your point? try isn't supposed to gobble up NoMethodErrors, it's meant to stop you from calling any method on a NilClass.Northeasterly

© 2022 - 2024 — McMap. All rights reserved.