A
Austin Ziegler
On Sun, 9 Jan 2005 05:53:24 +0900, Trevor Andrade
I don't know your language background. I do not consider this
"multiple assignment," even through from a literal perspective it
is. Perhaps "variable reassignment" is a better term. It is not a
common use of the term in the Ruby community.
You have demonstrated a fundamental misunderstanding of Ruby's
variables if you at all consider that:
x = x + 1
and
x.+(1)
could or should ever be the same. Not only is #+ not destructive,
Fixnums are immutable objects. Variables do not contain values, they
contain references to objects. Therefore, you cannot simply modify
a variable x containing a Fixnum value; you have to assign a new
value into the variable x.
Mutable objects (e.g., Strings, Arrays, Hashes, user defined
objects) could treat x.+(1) as "the same" as x += 1, but that's
because the internal state of the object is being changed. It's
still not advisable, because as stated, #+ is usually a non-
destructive form. In Ruby, if you want to concatenate strings, you
have three choices:
"#{a}#{b}" # Creates a new string
a + b # Creates a new string
a << b # Modifies the string referenced by 'a'
Variables, though, aren't objects. They're just slots.
-austin
I was just wondering how do they fit together? Are you talking about
multiple assignment as in:
X = 1
X = 2 [...]
I think was a bit ambiguous in my email so let me make myself
clear. The term multiple assignment is used in two situation or at
least I use it in two situations. In one situation a variable is
assigned to and then assigned to again. In the other situation,
two variable are assigned to at the same time. Both these
situations are called multiple assignment. I am referring to the
first situation. I believe you are referring to the second. I have
no problem with the second situation. It is the first that I
believe is unnecessary. Unless I am missing something and the two
kinds of multiple assignment are some how related.
I don't know your language background. I do not consider this
"multiple assignment," even through from a literal perspective it
is. Perhaps "variable reassignment" is a better term. It is not a
common use of the term in the Ruby community.
You have demonstrated a fundamental misunderstanding of Ruby's
variables if you at all consider that:
x = x + 1
and
x.+(1)
could or should ever be the same. Not only is #+ not destructive,
Fixnums are immutable objects. Variables do not contain values, they
contain references to objects. Therefore, you cannot simply modify
a variable x containing a Fixnum value; you have to assign a new
value into the variable x.
Mutable objects (e.g., Strings, Arrays, Hashes, user defined
objects) could treat x.+(1) as "the same" as x += 1, but that's
because the internal state of the object is being changed. It's
still not advisable, because as stated, #+ is usually a non-
destructive form. In Ruby, if you want to concatenate strings, you
have three choices:
"#{a}#{b}" # Creates a new string
a + b # Creates a new string
a << b # Modifies the string referenced by 'a'
Variables, though, aren't objects. They're just slots.
-austin