Type inference in ruby

A

Austin Ziegler

On Sun, 9 Jan 2005 05:53:24 +0900, Trevor Andrade
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
 
T

Trevor Andrade

You are correct. I have fundamentally misunderstood the ruby language. It
is not the variables I misunderstood though. I have fundamentally
misunderstood the operator + and the fact that Fixnums are not modifiable.
+ is non-destructive and that implies that it is impossible to get rid of

x = x + 1

For some reason I though x.+(1) would change the internal state of the
object referred to by x which it doesn't of course, it just returns a new
object. The only way to eliminate x = x + 1 is to make '+' destructive but
this of course cause many problems like the following

a = 1
x = a+3

Now 'a' would have the value 4 which is of course bad. Therefore I think I
was wrong and single variable assignment is impossible in Ruby. You have to
allow variable reassignment in Ruby.

Thanks.

-----Original Message-----
From: Austin Ziegler [mailto:[email protected]]
Sent: Sunday, January 09, 2005 1:39 PM
To: ruby-talk ML
Subject: Re: Type inference in ruby

On Sun, 9 Jan 2005 05:53:24 +0900, Trevor Andrade
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top