访问对象#try
我正在尝试访问 ruby 中的 Object#try。
我认为仅包含 activesupport 就可以解决问题,但事实并非如此。
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>
如何访问 Object#try
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要尝试,您需要做的就是
require "active_support/core_ext/object/try"
。 源代码在这里并且因为NilClass
被打开并且try
被定义为返回nil,所以你获得了链接能力。To get try all you need to do is
require "active_support/core_ext/object/try"
. The source is here and because theNilClass
is opened andtry
is defined to return nil, you get the chaining ability.