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 UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
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)