Read more

Ruby 1.9 or Ruby 2.0 do not allow using shortcut blocks for private methods

Arne Hartherz
March 15, 2013Software engineer at makandra GmbH

Consider this class:

class Foo

  private
  
  def test
    puts "Hello"
  end
  
end
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

While you can say create a block to call that method (using ampersand and colon) on Ruby 1.8, ...

1.8.7 > Foo.new.tap(&:test)
Hello
=> #<Foo:0x1e253c8> 

... you cannot do that on Ruby 1.9 or 2.0:

1.9.3 > Foo.new.tap(&:test)
NoMethodError: private method `test' called for #<Foo:0x00000001e8c258>

^
2.0.0 > Foo.new.tap(&:test)
NoMethodError: private method `test' called for #Foo:0x000000027bc738

The method is private after all, so there is not really a reason to call it from the outside in most cases. :)

Posted by Arne Hartherz to makandra dev (2013-03-15 14:03)