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

Posted About 11 years ago. Visible to the public.

Consider this class:

class Foo

  private
  
  def test
    puts "Hello"
  end
  
end

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. :)

Arne Hartherz
Last edit
About 11 years ago
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2013-03-15 13:03)