Last active
August 29, 2015 13:58
-
-
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?
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 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]> |
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 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