Created
June 25, 2020 02:46
-
-
Save formigarafa/ff5ccb15fea0843c66374cb704967e53 to your computer and use it in GitHub Desktop.
Ruby 2.6.6 weird behaviour for protected method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item | |
def initialize(value: ) | |
@value = value | |
end | |
def subitems | |
@subitems ||= [ | |
Item.new(value: 1), | |
Item.new(value: 2), | |
] | |
end | |
def aggregated_total1 | |
subitems.map{|i| i.value}.sum + value | |
end | |
def aggregated_total2 | |
subitems.map(&:value).sum + value | |
end | |
protected | |
def value | |
@value | |
end | |
end | |
RUBY_DESCRIPTION # => "ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-darwin19]" | |
Item.new(value: 0).aggregated_total1 # => 3 | |
Item.new(value: 0).aggregated_total2 # NoMethodError: protected method `value' called for #<Item:0x00007fc5217b0258 @value=1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment