Skip to content

Instantly share code, notes, and snippets.

@formigarafa
Created June 25, 2020 02:46
Show Gist options
  • Save formigarafa/ff5ccb15fea0843c66374cb704967e53 to your computer and use it in GitHub Desktop.
Save formigarafa/ff5ccb15fea0843c66374cb704967e53 to your computer and use it in GitHub Desktop.
Ruby 2.6.6 weird behaviour for protected method
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