Read more

instance_eval behaves different in Ruby 1.8 and Ruby 1.9, use instance_exec instead

Henning Koch
March 02, 2013Software engineer at makandra GmbH

In Ruby 1.9, instance_eval calls the block the with receiver as the first argument:

  • In Ruby 1.8, receiver.instance_eval(&block) calls block.call()
  • In Ruby 1.9, receiver.instance_eval(&block) calls block.call(receiver)
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

This will blow up in your face in Ruby 1.9, where a lambda crashes when it is called with a different number of arguments:

wrong number of arguments (1 for 0) (ArgumentError)

Forget that instance_eval ever existed. Use instance_exec instead, which behaves consistently across all Rubies.

Posted by Henning Koch to makandra dev (2013-03-02 09:47)