Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@advorak
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advorak/10023642 to your computer and use it in GitHub Desktop.
Save advorak/10023642 to your computer and use it in GitHub Desktop.
is it appropriate in a class, for a getter method of an instance variable which happens to be an array, to dup the variable so that such things as getter#push cannot be called on them?
class Andy
def initialize
@andytest = []
end
attr_reader :andytest
end
__END__
irb(main):017:0> x=Andy.new
=> #<Andy:0x007f25fec58b38 @andytest=[]>
irb(main):018:0> x.andytest
=> []
irb(main):019:0> x.andytest.push 1
=> [1]
irb(main):020:0> x
=> #<Andy:0x007f25fec58b38 @andytest=[1]>
class Andy
def initialize
@andytest = []
end
def andytest
@andytest.dup
end
end
__END__
irb(main):013:0* x=Andy.new
=> #<Andy:0x007f0249208918 @andytest=[]>
irb(main):014:0> x.andytest
=> []
irb(main):015:0> x.andytest.push 1
=> [1]
irb(main):016:0> x
=> #<Andy:0x007f0249208918 @andytest=[]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment