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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)