不明白爲什麼我在運行此程序時得到堆棧級別太深。堆棧級別太深(SystemStackError),而使用class_eval ruby
module A
class Fruit
def initialize
puts "pears"
end
[:orange, :apple].each do |fruit|
class_eval %Q{
def #{fruit}
puts #{fruit}
end
}
end
puts "pineapple"
end
a_fruit = Fruit.new
a_fruit.apple
end
another_fruit = A::Fruit.new
another_fruit.orange
該程序的輸出是
(eval):3:in `apple': stack level too deep (SystemStackError)
from (eval):3:in `apple'
from testquestion.rb:20