我如何在 ruby 中创建一个在类似于 nil 的逻辑表达式中被评估为 false 的对象?
我的意图是启用对其他对象的嵌套调用,在这些对象中,链的中间某处的值通常是 nil
,但允许所有调用继续 - 返回我的类 nil 对象而不是 nil
本身。该对象将返回自身以响应它不知道如何处理的任何收到的消息,我预计我将需要实现一些覆盖方法,例如 nil?
.
例如:
fizz.buzz.foo.bar
如果buzz
fizz
的属性(property)不可用我会返回我的类似 nil 的对象,它会接受一直到 bar
的调用回归自身。最终,上面的陈述应该评估为 false。
编辑:
根据下面所有很好的答案,我得出以下结论:
class NilClass
attr_accessor :forgiving
def method_missing(name, *args, &block)
return self if @forgiving
super
end
def forgive
@forgiving = true
yield if block_given?
@forgiving = false
end
end
这允许像这样的一些卑鄙的技巧:
nil.forgiving {
hash = {}
value = hash[:key].i.dont.care.that.you.dont.exist
if value.nil?
# great, we found out without checking all its parents too
else
# got the value without checking its parents, yaldi
end
}
显然,您可以将这个 block 透明地包装在某个函数调用/类/模块/任何地方。
最佳答案
这是一个很长的答案,其中包含大量关于如何解决问题的想法和代码示例。
尝试
Rails 有一个 try method让你像那样编程。这是它的实现方式:
class Object
def try(*args, &b)
__send__(*a, &b)
end
end
class NilClass # NilClass is the class of the nil singleton object
def try(*args)
nil
end
end
你可以像这样用它编程:
fizz.try(:buzz).try(:foo).try(:bar)
你可以想像地修改它以稍微不同地工作以支持更优雅的 API:
class Object
def try(*args)
if args.length > 0
method = args.shift # get the first method
__send__(method).try(*args) # Call `try` recursively on the result method
else
self # No more methods in chain return result
end
end
end
# And keep NilClass same as above
然后你可以这样做:
fizz.try(:buzz, :foo, :bar)
andand
andand使用了一种更邪恶的技术,破解了不能直接实例化 NilClass 子类的事实:
class Object
def andand
if self
self
else # this branch is chosen if `self.nil? or self == false`
Mock.new(self) # might want to modify if you have useful methods on false
end
end
end
class Mock < BasicObject
def initialize(me)
super()
@me = me
end
def method_missing(*args) # if any method is called return the original object
@me
end
end
这允许您以这种方式编程:
fizz.andand.buzz.andand.foo.andand.bar
结合一些花哨的重写
您可以再次扩展此技术:
class Object
def method_missing(m, *args, &blk) # `m` is the name of the method
if m[0] == '_' and respond_to? m[1..-1] # if it starts with '_' and the object
Mock.new(self.send(m[1..-1])) # responds to the rest wrap it.
else # otherwise throw exception or use
super # object specific method_missing
end
end
end
class Mock < BasicObject
def initialize(me)
super()
@me = me
end
def method_missing(m, *args, &blk)
if m[-1] == '_' # If method ends with '_'
# If @me isn't nil call m without final '_' and return its result.
# If @me is nil then return `nil`.
@me.send(m[0...-1], *args, &blk) if @me
else
@me = @me.send(m, *args, &blk) if @me # Otherwise call method on `@me` and
self # store result then return mock.
end
end
end
解释一下发生了什么:当您调用带下划线的方法时,您会触发模拟模式,_meth
的结果会自动包装在 Mock
对象中。任何时候你在那个 mock 上调用一个方法,它都会检查它是否没有持有 nil
,然后将你的方法转发给那个对象(这里存储在 @me
变量中)。模拟然后用您的函数调用的结果替换原始对象。当您调用 meth_
时,它会结束模拟模式并返回 meth
的实际返回值。
这允许像这样的 api(我使用下划线,但你可以使用任何东西):
fizz._buzz.foo.bum.yum.bar_
残酷的猴子修补方法
这真的很讨厌,但它允许一个优雅的 API,并且不一定会在整个应用程序中搞砸错误报告:
class NilClass
attr_accessor :complain
def method_missing(*args)
if @complain
super
else
self
end
end
end
nil.complain = true
像这样使用:
nil.complain = false
fizz.buzz.foo.bar
nil.complain = true
关于Ruby 类 nil 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8766191/