新手问题 不是很懂 undef 怎么用

u4crella · 2019年11月28日 · 最后由 spike76 回复于 2019年11月28日 · 2300 次阅读

怎么 取消 后来通过 eval 方式添加的 f(x) 方法?

class Heval
  def initialize
  end
  def he(text)
      eval(text)
  end
#  def inside_undef(text)
#    undef :f
#  end
end

str1 = 'def f(x)
return (x + 1)
end'
hh = Heval.new
hh.he(str1)
puts hh.f(2) # => 3

#hh.inside_undef('f')
#undef f #undef self.f
puts hh.respond_to?(:he) # => true
puts hh.respond_to?(:f) #=> true


__END__
How to cancel the manual 'f(x)' definition in the hh class_instance?

Heval.class_eval { undef :f }

需要 登录 后方可回复, 如果你还没有账号请 注册新账号