Please don't flame me...why is there no "++" in Ruby again?

David M. wrote:

On Saturday 30 August 2008 13:11:38 Erik H. wrote:

David M. wrote:

Why not add a ++ that works the same way += does?

1++ would have to operate (essentially) like 1.succ!

Except 1++ wouldn’t work, any more than 1 += 1 would work.

But it’s by no means impossible, or a fundamental change to the
language.
All it does is make the parser more complex.

You’re right, and boy is that an embarassing omission on my part. :slight_smile:

-Erik

On Saturday 30 August 2008 13:11:38 Erik H. wrote:

David M. wrote:

Why not add a ++ that works the same way += does?

1++ would have to operate (essentially) like 1.succ!

Except 1++ wouldn’t work, any more than 1 += 1 would work.

Again, the suggestion was that it work the same way += does – and +=
doesn’t
rely on a .succ! method of any kind. It would be purely syntactic sugar,
the
way +=.

In other words, it would still parse to:

foo = foo + 1

Or, maybe slightly better, it would parse to:

foo = foo.succ

it would just be shorter to write than

foo += 1

The immutability of integers is a good thing for people to know, yes.
It’s also completely irrelevant to this discussion.

And for the record, I’m playing devil’s advocate – the real issue seems
to be
that ++ would add to the parsing complexity of it. I’m guessing most of
us
rarely do +=1, so most of us wouldn’t really use it, so it’s not worth
that
complexity.

But it’s by no means impossible, or a fundamental change to the
language.
All it does is make the parser more complex.

John Pritchard-williams wrote:

There’s must be very good simple reason why there is no ‘x++’ method
Ruby right?

Just generally interested why there isn’t…

I did check my Ruby books by the way, but they just “unlike C there is
no ++ operator in Ruby…” :slight_smile:

Many good answers already. I’d like to point out yet another reason why.
Consider:
for(i=0; i<100; i++) { loop_body }

Here, i is incremented after the loop_body. Thus its name,
post-increment. In ruby there is no way to tell a method to run not
where it lexically is but after some action. You couldn’t even identify
after what action. After the next yield? After the next statement?

Regards
Stefan (apeiros)

On Thu, Aug 28, 2008 at 11:56 PM, John Pritchard-williams
[email protected] wrote:

increment it and re-point ‘a’ at that new object"…

but then people might abuse it and not realize (as I hadn’t really) that
Integers are immutable…and give the garbage collector a really hard
time…
Do you mean, people should not write
a = a + 1
because of GC issues???
Robert

C’est véritablement utile puisque c’est joli.

Antoine de Saint Exupéry