简体   繁体   中英

Differences in instance_eval behaviour between Ruby 1.9.1 and 1.8.6?

I noticed that class variables @@my_class var are now looked up in the context of the instance_eval'd object in Ruby 1.9.1 whereas this was not the case in Ruby 1.8.6.

What are some other differences in behaviour of instance_eval for Ruby 1.9.1 ?

I can't list them all, but I know that there are some changes in constant lookup. In 1.8, constant lookup was in the callers scope, while it is in the blocks scope in 1.9.

FOO = "hi"

class Something
  FOO = "bye"

  def self.handle(&block)
    instance_eval(&block)
  end
end

p Something.handle { FOO }
# "hi" on 1.8, "bye" on 1.9

我认为(至少目前)它也不会返回自我。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM