Project

General

Profile

Actions

Bug #16403

closed

Unavailable protected methods through `Symbol#to_proc`

Added by AlexWayfer (Alexander Popov) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
[ruby-core:96120]

Description

Hello!

I've faced with a bug (I guess):

# protected.rb
# frozen_string_literal: true

class Foo
  def initialize(text, *children)
    @text = text
    @children = children
  end

  def via_each
    @children.reduce(text) do |result, child|
      result + child.text
    end
  end

  def via_map
    text + @children.map(&:text).reduce(:+)
  end

  protected

  attr_reader :text
end

child1 = Foo.new('Alexander ')
child2 = Foo.new('Popov')

foo = Foo.new('Hello ', child1, child2)

puts foo.via_each

puts foo.via_map

Output:

Hello Alexander Popov
Traceback (most recent call last):
	2: from protected.rb:31:in `<main>'
	1: from protected.rb:16:in `via_map'
protected.rb:16:in `map': protected method `text' called for #<Foo:0x000055db24b97710 @text="Alexander ", @children=[]> (NoMethodError)

Updated by nobu (Nobuyoshi Nakada) over 4 years ago

  • Status changed from Open to Rejected

Proc by Symbol#to_proc calls the method on its argument in an isolated context, so the receiver does not exist.

Actions

Also available in: Atom PDF

Like0
Like0