I had a theoretical question about Ruby's object handling. Not that it really matters, but I'm using Ruby 1.8.6.
In Ruby, every variable holds a reference to an object. For instance, if you were to say:
Expand|Select|Wrap|Line Numbers
- myVar1 = []
- myVar2 = myVar1
- myVar2 << "giggity giggity goo"
- myVar[0] # => "giggity giggity goo"
However, this creates an interesting dilemma with regards to attribute readers, because technically, you can modify what should be a protected object. Normally this isn't a problem, but in the case that the object has mutating/destructive methods, they can be used. Consider the following class:
Expand|Select|Wrap|Line Numbers
- class Retarded
- attr_reader :var
- def initialize
- @var = "mystring \n"
- end
- end
- x = Retarded.new
- x.var.chomp! # => "mystring "
- x.var # => "mystring " the internal variable has changed
Thanks!