YA ++ explanation

I’ve worked it out in my mind why ++ isn’t valid. Maybe my thoughts will
help others. And maybe someone can straighten me out if I’m wrong!

Following is an annotated irb session:

irb(main):001:0> a=“abc”
“abc”
irb(main):002:0> a.id # The “abc” string has a particular ID
22443468
irb(main):003:0> a.succ!# succ!() is only similar to ++ for numbers
“abd” # but is actually a well defined method
irb(main):004:0> a.id # for introducing an effect on the string.
22443468 # Interestingly, we KEEP the same instance
# and only the contents had to change!
irb(main):005:0> a += “efg”
“abdefg”
irb(main):006:0> a
“abdefg”
irb(main):007:0> a.id # With the above operation, we had to get a new
22410024 # instance since we did an assignment.

···

irb(main):008:0> b=123 # Now consider Integers:
123 #
irb(main):009:0> b.id # Now we have a particular Integer object.
247 # Indeed, all 123 integers will have this ID
irb(main):010:0> b++ #
irb(main):011:0* # As you can see, this op is invalid. To be
irb(main):012:0* # like succ!, it would require keeping the
irb(main):013:0* d # SAME object but altering that objects CONTENTS
# (said contents being the immutable number 123).
# Compare the annotations for a.succ! and b++
# to convince yourself.
Error: … # But Integer values are immutable!
# Otherwise, the number 3 could become 4 and
# you’d end up with a silly Monty Python skit.
# [“1, 2, 4” “NO, 3, Sir!” “OH, 3!” BOOM!]
#
irb(main):014:0> b += 5 # Now this operation works because the language
128 # ‘knows’ how to find the item in the set of
irb(main):015:0> b.id # Integer values that is ‘5’ beyond ‘123’ and
257 # Ruby then returns then assigns the reference
# of this newly found ‘128’ object to the
# variable. NOTE: assignment of a new instance
# has happened just as in the String += op.

The important part of the explanation is the b++ annotation.

I hope I have it right. And if I do have it right, I hope it’s the kind of
explanation that makes sense to someone else and can enlighten.

Good day.

±-----------------------------------------------+

DREW MILLS | 10101 Linn Station Rd. |
> Suite 800 |
tamills@ups.com | Louisville, KY 40223 |
Technical Specialist | 502-394-7785 |
United Parcel Service | 502-394-7812 |
±-----------------------------------------------+

Hi,

Any comments? Since everything in ruby is object, += is a method of the
object preceding it, why can’t ++ like that? it can be defined to avoid
the “immutable” property discussed, anyway. A little more confused… I
guee I have asked this question…

Shannon

···

On Sat, 7 Dec 2002 06:08:47 +0900 “Mills Thomas (app1tam)” app1tam@ups.com wrote:

I’ve worked it out in my mind why ++ isn’t valid. Maybe my thoughts will
help others. And maybe someone can straighten me out if I’m wrong!

Following is an annotated irb session:

irb(main):001:0> a=“abc”
“abc”
irb(main):002:0> a.id # The “abc” string has a particular ID
22443468
irb(main):003:0> a.succ!# succ!() is only similar to ++ for numbers
“abd” # but is actually a well defined method
irb(main):004:0> a.id # for introducing an effect on the string.
22443468 # Interestingly, we KEEP the same instance
# and only the contents had to change!
irb(main):005:0> a += “efg”
“abdefg”
irb(main):006:0> a
“abdefg”
irb(main):007:0> a.id # With the above operation, we had to get a new
22410024 # instance since we did an assignment.
#
irb(main):008:0> b=123 # Now consider Integers:
123 #
irb(main):009:0> b.id # Now we have a particular Integer object.
247 # Indeed, all 123 integers will have this ID
irb(main):010:0> b++ #
irb(main):011:0* # As you can see, this op is invalid. To be
irb(main):012:0* # like succ!, it would require keeping the
irb(main):013:0* d # SAME object but altering that objects CONTENTS
# (said contents being the immutable number 123).
# Compare the annotations for a.succ! and b++
# to convince yourself.
Error: … # But Integer values are immutable!
# Otherwise, the number 3 could become 4 and
# you’d end up with a silly Monty Python skit.
# [“1, 2, 4” “NO, 3, Sir!” “OH, 3!” BOOM!]
#
irb(main):014:0> b += 5 # Now this operation works because the language
128 # ‘knows’ how to find the item in the set of
irb(main):015:0> b.id # Integer values that is ‘5’ beyond ‘123’ and
257 # Ruby then returns then assigns the reference
# of this newly found ‘128’ object to the
# variable. NOTE: assignment of a new instance
# has happened just as in the String += op.

The important part of the explanation is the b++ annotation.

I hope I have it right. And if I do have it right, I hope it’s the kind of
explanation that makes sense to someone else and can enlighten.

Good day.

±-----------------------------------------------+

DREW MILLS | 10101 Linn Station Rd. |
> Suite 800 |
tamills@ups.com | Louisville, KY 40223 |
Technical Specialist | 502-394-7785 |
United Parcel Service | 502-394-7812 |
±-----------------------------------------------+

Any comments? Since everything in ruby is object, += is a method of the
object preceding it, why can’t ++ like that?

The operator += is not a method in Ruby. The parser treats += in a special
way. Specifically, it treats:

a += x

As an alias for:

a = a + x

In that form, the + operator is a method call on the object refered to by
‘a’ with the object refered to by ‘b’ as the parameter. The ‘=’ is an
assignment to a local variable ‘a’, and is handled in a special way by the
language.

Cheers,
Nat.

···

From: “Shannon Fang” xrfang@hotmail.com

I think he had ‘succ!’ in his mind.

···

On Sat, Dec 07, 2002 at 06:39:56AM +0900, Nat Pryce wrote:

From: “Shannon Fang” xrfang@hotmail.com

Any comments? Since everything in ruby is object, += is a method of the
object preceding it, why can’t ++ like that?

The operator += is not a method in Ruby. The parser treats += in a special
way. Specifically, it treats:

a += x

As an alias for:

a = a + x

In that form, the + operator is a method call on the object refered to by
‘a’ with the object refered to by ‘b’ as the parameter. The ‘=’ is an
assignment to a local variable ‘a’, and is handled in a special way by the
language.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

…Deep Hack Mode – that mysterious and frightening state of
consciousness where Mortal Users fear to tread.
– Matt Welsh

The reason I am considering this is I try to understand how operator
overload work in ruby, could anyone tell me a list of all operators that
I can overload in ruby?

Tks…
Shannon

···

On Sat, 7 Dec 2002 07:12:27 +0900 Mauricio Fern~{an~}dez batsman.geo@yahoo.com wrote:

On Sat, Dec 07, 2002 at 06:39:56AM +0900, Nat Pryce wrote:

From: “Shannon Fang” xrfang@hotmail.com

Any comments? Since everything in ruby is object, += is a method of the
object preceding it, why can’t ++ like that?

The operator += is not a method in Ruby. The parser treats += in a special
way. Specifically, it treats:

a += x

As an alias for:

a = a + x

In that form, the + operator is a method call on the object refered to by
‘a’ with the object refered to by ‘b’ as the parameter. The ‘=’ is an
assignment to a local variable ‘a’, and is handled in a special way by the
language.

I think he had ‘succ!’ in his mind.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

…Deep Hack Mode – that mysterious and frightening state of
consciousness where Mortal Users fear to tread.
– Matt Welsh

Shannon Fang wrote:

The reason I am considering this is I try to understand how operator
overload work in ruby, could anyone tell me a list of all operators that
I can overload in ruby?

See http://ruby-talk.org/57460