From: Gavin Sinclair Date: 2002-11-26T12:49:42+09:00 Subject: Re: Things That Newcomers to Ruby Should Know (11/24/02) From: "Daniel Carrera" > [snip] > > 4 is an unmodifiable object. "Hello".chomp! is fine because "Hello" is a > > modifiable object. > > Thanks, I'm understanding more now. I guess that the next logical > question is why is 4 unmodifiable and "a" modifiable? I don't see why a > string would be modifiable and a number not. > > Just like you can say that the number 4 is set, and cannot be turned into > a 5, you can also say that the letter "a" is set and cannot be turned into > a "b". Yet, I can write: > > "a".gsub!(/a/,"b") > > Isn't it then arbitrary to declare numbers unmodifiable but declare > strings modifiable? Perhaps Matz has a good reason for this. > > Daniel. It's for performance, mainly (exlusively?). In Java, integers are fixed quantities. In Ruby, integers are fixed quantities with objectlike properties. Besides, numbers are the bread and butter of programming. Could you imagine, instead of x = 4 you had to do x = Integer.new(4) ? Even if the former were syntax sugar for the latter, it would still be a bit strange. Maybe I'm just used to it. I have no conceptual problem with the difference between numbers and Strings. "a" is not a string, per se. It's an object whose data is considered to be a string. Changing the "a" to a "b" is merely changing the data of the object. Perhaps some people would argue that the same should be true for numbers, but I doubt anyone's life would change if it were. Gavin