class C
attr_accessor :v
def initialize(arg)
@v = arg
end
def meth(arg=nil)
return arg.meth unless (arg.nil?) && !(arg.is_a? self.class)
@v
end
end
如果arg 不是 nil 并且 if ,meth
我想做。否则返回。但我想这部分是不正确的。无法弄清楚出了什么问题。arg.meth
arg.is_a?C
@v
!(arg.is_a? self.class)
x = C.new("first")
y = C.new("second")
x.meth #=> "first"
x.meth(y) #=> "second"
x.meth(10) #=> I was expecting 'first'
But I get NoMethodError: undefined method `meth' for 10:Fixnum
我不想否定arg.nil?
我想实现arg.is_not_a?
更新:看起来我的问题令人困惑。我想在这里澄清
if the argument passed to meth is not nil and is of type C then
call arg.meth
else
return @v
end