There is nothing wrong with modifying self
, you cannot assign it to it, but you can use access methods to change it or change instance variables if you have direct access to them. String#replace
is an accessory in this particular case.
Another example:
class Array def delete_first_element! self[0..0] = [] end end a = [1,2,3] b = a a.delete_first_element! puts a.inspect # [2,3] puts b.inspect # [2,3]!
One thing to remember is that you change all the links of the same object ( b
in my example)!
source share