instance_eval's block argument(s)- documented? purpose?

前端 未结 3 1787
悲&欢浪女
悲&欢浪女 2021-01-12 11:28

Just realized that instance_eval yields self as an argument to the associated block (except for a bug in the 1.9.2 version: http://www.ruby-forum.c

3条回答
  •  萌比男神i
    2021-01-12 12:02

    I have just dicovered that unlike #instance_eval, which is primarily intended for string evaluation, #instance_exec primarily intended for block evaluation, does not have the described behavior:

    o = Object.new
    o.instance_exec { |*a| puts "a.size is #{a.size}" }
      => a.size is 0
    

    This is probably an unintended inconsistency, so you might have discovered a bug. Post it on Ruby bugs.

提交回复
热议问题