3

I have been using Ruby on Rails for a while without studying Ruby, now I am taking Odin Project. I am not really sure about the answer of this question: What does it mean that strings are "mutable" and why care?

Update: so now I understand mutable string basically means the value in the memory can be changed after string is created.

immutable string means the value in the memory cannot be changed once created, only the reference can be changed.

based on the result of following code:

a = "foo"
a.object_id
 => 70218039369160
b = "bar"
a << b
 => "foobar"
a.object_id
 => 70218039369160

can I say string in Ruby is mutable? because the value in same memory is changed

a += b
 => "foobar"
a.object_id
 => 70218039184800

and the + method in Ruby actually create a new String object instead of change the value of the original String object, that's why the object id changed.

my question is will it cause any security problem if I use += and << interchangeably?

Zhiliang Xing
  • 1,057
  • 1
  • 8
  • 19
  • 1
    This http://stackoverflow.com/questions/8580304/are-strings-in-ruby-mutable question shows what it means and linked http://stackoverflow.com/questions/2608493/why-did-matz-choose-to-make-strings-mutable-by-default-in-ruby goes into semi-opinionated discussion on why. – Alexei Levenkov Nov 13 '15 at 06:32
  • thanks a lot, thats exactly what I want – Zhiliang Xing Nov 13 '15 at 06:49
  • 1
    That's a rather big question. It's also one about which a lot has been written. See, for example, [this article](http://davesrubyrails.blogspot.ca/2013/12/why-strings-are-mutable-in-ruby.html). For those reasons, I don't think it's a suitable question for SO, as it would be difficult to give an answer that is more informative than what Google will suggest when you type in "Ruby mutable strings" and hit Enter. Neither, therefore, is it a good use of members' time to write answers to this question. – Cary Swoveland Nov 13 '15 at 06:56
  • 1
    Please ask new question as you completely changed original one and invalidated the answer given to the post. – Alexei Levenkov Nov 13 '15 at 17:02

1 Answers1

1

It means that you can modify an exising instance of a string, without constructing a new one. Consider the following code:

str1 = "foo"
str2 = str1
str1 += "bar"

In languages like javascript where strings are immutable, the value of str2 will still be "bar" after that code is executed, as you can see here. However, in languages where strings are mutable, like ruby, when you append "bar" at the end of str1 (using the ruby operator << which does that) you are actually modifying the instance, not creating a new one, so str2 will also be modified, as you can see here.

PS: Note that the append at the end of the string operator in ruby is << instead of += (+= actually creates a new string, but not because it's forced to do it, like in javascript).

Hawkings
  • 533
  • 3
  • 16
  • Sample looks quite backward... Please check your code (`str1+="bar"` does not change string and str2 is still "foo") – Alexei Levenkov Nov 13 '15 at 06:56
  • @AlexeiLevenkov I don't understand you, the sample codes work as expected, taking into account that in ruby you have to use the operator `<<`, as I clarified in my answer. – Hawkings Nov 13 '15 at 16:02
  • " like ruby, when you execute str1 += "bar" you are actually modifying the instance" - I get that you trying to say that `+=` and `<<` are similar but completely different, but that is not clearly reflected inline in your post. (To some extent that does not matter as posts I've added as comments explain difference well enough...) – Alexei Levenkov Nov 13 '15 at 17:05
  • While strings are mutable, symbols are not: http://www.reactive.io/tips/2009/01/11/the-difference-between-ruby-symbols-and-strings/ – B Seven Nov 13 '15 at 17:59