Fin*_*arr 12 ruby metaprogramming null-object-pattern
如何在ruby中创建一个Object,在类似于nil的逻辑表达式中将其评估为false?
我的目的是在其他对象上启用嵌套调用,其中某个值通常是链的一半nil
,但允许所有调用继续 - 返回我的类似nil的对象而不是nil
自身.该对象将返回自身以响应任何收到的消息,它不知道如何处理,我预计我将需要实现一些覆盖方法,如nil?
.
例如:
fizz.buzz.foo.bar
Run Code Online (Sandbox Code Playgroud)
如果buzz
属性fizz
不可用,我会返回我的类似nil的对象,它会一直接受调用以bar
返回自身.最终,上述陈述应评估为假.
编辑:
根据以下所有优秀答案,我提出了以下建议:
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
Run Code Online (Sandbox Code Playgroud)
这允许一些卑鄙的技巧,如:
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
}
Run Code Online (Sandbox Code Playgroud)
显然你可以在一些函数调用/ class/module/where里透明地包装这个块.
这是一个很长的答案,有很多关于如何处理问题的想法和代码示例.
try
Rails有一个try方法,让你像那样编程.这就是它的实现方式:
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
Run Code Online (Sandbox Code Playgroud)
您可以像这样编程:
fizz.try(:buzz).try(:foo).try(:bar)
Run Code Online (Sandbox Code Playgroud)
你可以想象,修改它可以有所不同,以支持更优雅的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
Run Code Online (Sandbox Code Playgroud)
然后你可以这样做:
fizz.try(:buzz, :foo, :bar)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
这允许您以这种方式编程:
fizz.andand.buzz.andand.foo.andand.bar
Run Code Online (Sandbox Code Playgroud)
你可以再次扩展这种技术:
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
Run Code Online (Sandbox Code Playgroud)
要解释发生了什么:当你调用一个强调方法触发模拟模式时,结果_meth
会自动包装在一个Mock
对象中.无论何时你在该模拟上调用一个方法,它都会检查它是否没有持有a nil
然后将你的方法转发给该对象(这里存储在@me
变量中).然后mock使用函数调用的结果替换原始对象.当你调用meth_
它结束模拟模式并返回实际的返回值meth
.
这允许像这样的api(我使用了下划线,但你可以使用任何东西):
fizz._buzz.foo.bum.yum.bar_
Run Code Online (Sandbox Code Playgroud)
这真的非常讨厌,但它允许一个优雅的API,并不一定搞砸整个应用程序中的错误报告:
class NilClass
attr_accessor :complain
def method_missing(*args)
if @complain
super
else
self
end
end
end
nil.complain = true
Run Code Online (Sandbox Code Playgroud)
使用这样:
nil.complain = false
fizz.buzz.foo.bar
nil.complain = true
Run Code Online (Sandbox Code Playgroud)
据我所知,没有非常简单的方法可以做到这一点.在Ruby社区中已经完成了一些工作,它们实现了您正在谈论的功能; 你可能想看看:
andand gem的用法如下:
require 'andand'
...
fizz.buzz.andand.foo.andand.bar
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
931 次 |
最近记录: |