Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created August 12, 2014 05:23
Show Gist options
  • Save domgetter/966ccf3a52d1ff47747e to your computer and use it in GitHub Desktop.
Save domgetter/966ccf3a52d1ff47747e to your computer and use it in GitHub Desktop.
How do I prevent the @children array from being changed outside the class since I only used attr_reader ?
class Human
attr_reader :children
def initialize
@children = ["Joe", "Henry"]
end
end
#=> nil
human = Human.new
#=> #<Human:0x2a19bd8 @children=["Joe", "Henry"]>
human.children << "Mary"
#=> ["Joe", "Henry", "Mary"]
human
#=> #<Human:0x2a19bd8 @children=["Joe", "Henry", "Mary"]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment