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

u4crella · November 28, 2019 · Last by spike76 replied at November 28, 2019 · 2301 hits

怎么 取消 后来通过 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 }

You need to Sign in before reply, if you don't have an account, please Sign up first.