Post inc problem

Hi,

A quick and dumb question: does Ruby (1.6) support postincrementation like C++? I mean: " x++ " expression where x is a numeric variable.
Thanks,

Gábor

"Put your message in a modem and throw it in the Cyber Sea." - N. Peart

Hi,

A quick and dumb question: does Ruby (1.6) support postincrementation
like C++? I mean: " x++ " expression where x is a numeric variable.

Nope; all number variables reference number objects and you can't change
those. You can use:

x += 1
x = x + 1
x = x.succ
x = x.next

Thanks,

Gábor

E

···

On Thu, March 3, 2005 6:14 am, Sebestyén Gábor said:

Hi,

A quick and dumb question: does Ruby (1.6) support postincrementation
like C++? I mean: " x++ " expression where x is a numeric variable.
Thanks,

In ruby every operation is a method call. You can make this visible by writing

5.+(5)

So a ++ operation would mean change 5 to be 6. And that would be quite
inconsistent:
Numbers: 1,2,3,4,5,6,7....
5++
Numbers: 1,2,3,4,6,6,7...

:wink:

So no chance you will ever find r++ in ruby.

hope to help,

Brian

···

On Thu, 3 Mar 2005 15:14:44 +0900, Sebestyén Gábor <segabor@chello.hu> wrote:

Gábor

--
Brian Schröder
http://ruby.brian-schroeder.de/

Five introductory remarks: I dislike the ++ operator in C, etc., I don't like
the Perl TIMTOWTDI philosophy (makes it too hard for me to read Perl code,
especially since I gave up on learning Perl), and I don't know how I talked
myself into attempting to learn Ruby which has sometimes been described, in
some sense, as somewhat Perl like, and I'm too much of a newbie in Ruby to
think of the right (best) examples, and I'm not specifically responding to
Brian--I've seen similar answeres to this question a few times on the list,
but:

Doesn't Ruby somewhat pride itself on doing the intuitive thing, sometimes
including (for example) more than one syntax for the same thing to make it
more intuitive for newbies (like the ability to write a "normal looking" for
statement instead of requring the something.each syntax)?

Aren't there other cases somewhat like that--iiuc, a string is normally
mutable but you force it to be non-mutable (with, IIUC, the ! to make
destructive operations (if I have the terminology right)?

Is the "++" syntax used for some other operation, thus precluding its use for
a synonym for increment. And wouldn't the right thing for ++ be to increment
to the next number?

So why did I write this (and, will I press send? clearly, if you're reading
this I did press send)? Am I just ranting? Maybe. Do I really want "++"
added to the Ruby syntax? No, absolutely not!

Ok, maybe I have to go back to one of the statements attributed to Matz,
something like (paraphrased from memory): "I have tried to make Ruby
intuitive for me, I hope it is intuitive for others as well, but" he seems to
make no promises (iirc).

So, maybe I'm trying to come up with another explanation for why "++" will not
be supported in Ruby (with no sarcasm/disrepect intended), and maybe there
are at least two: One is the one mentioned above, that in the existing and
preferred (consistent) syntax of Ruby, ++ woud be interpreted as attempting
to change the object 5 to the object 6, and Matz's world view doesn't see
enough value in the == syntax to make it an exception. (100% fine by me, by
the way.)

So, again, shall I press send? Yes, I think so. I think the extra perspective
of knowing there is a person (at least nominally) in charge who has made a
conscious decision will let me (in my own mind) set the issue to rest. (That
is ignoring all the discussion I see (is that on a different list?) about
requests to change, among other things, the syntax of Ruby.

sorry for the noise,
Randy Kramer

···

On Thursday 03 March 2005 04:54 am, Brian Schröder wrote:

So a ++ operation would mean change 5 to be 6. And that would be quite
inconsistent:
Numbers: 1,2,3,4,5,6,7....
5++
Numbers: 1,2,3,4,6,6,7...

This comes up every so often, and as much as it has to do with the
problem of mutating a fixed value (e.g., 5++), it has much more to
do with the understanding of Ruby variables and the true nature of
object orientation in Ruby, as well as operator overriding.

At least, IMO.

There's two ways to implement ++. The first way is to implement it
as an operator, like #+ and #*. Because it is a unary operator, it
would probably be something like:

  def @++
    # implementation
  end

OK. That's fine. That's probably the better way, but then you've got
the question of -- how do you distinguish between prefix and
postfix? So now, you need *two* operators instead of one -- because
you know that people will use both (they have different meanings).
But you have to define both if you want one.

Ouch.

Okay, the other way is as syntax sugar. Define "a++" such that it is
the same as "a += 1". But what if a is a String? Your program blows
up. So we're back to option 1.

-austin

···

On Thu, 3 Mar 2005 22:07:44 +0900, Randy Kramer <rhkramer@gmail.com> wrote:

Aren't there other cases somewhat like that--iiuc, a string is
normally mutable but you force it to be non-mutable (with, IIUC,
the ! to make destructive operations (if I have the terminology
right)?

Is the "++" syntax used for some other operation, thus precluding
its use for a synonym for increment. And wouldn't the right thing
for ++ be to increment to the next number?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

> Is the "++" syntax used for some other operation, thus precluding
> its use for a synonym for increment. And wouldn't the right thing
> for ++ be to increment to the next number?

This comes up every so often, and as much as it has to do with the
problem of mutating a fixed value (e.g., 5++), it has much more to
do with the understanding of Ruby variables and the true nature of
object orientation in Ruby, as well as operator overriding.

At least, IMO.

There's two ways to implement ++. The first way is to implement it
as an operator, like #+ and #*. Because it is a unary operator, it
would probably be something like:

  def @++
    # implementation
  end

OK. That's fine.

Maybe I didn't understand you, but how would such an implementation
look like? The problem as I see it, is that we are sending messages to
the object and not to the variable. So we can't increment the
variable, because we don't know it.

z = y = x = 5 => 5
x.++ => 6

Now which value does z have, and which value does x have?

regards,

Brian

···

That's probably the better way, but then you've got
the question of -- how do you distinguish between prefix and
postfix? So now, you need *two* operators instead of one -- because
you know that people will use both (they have different meanings).
But you have to define both if you want one.

Ouch.

Okay, the other way is as syntax sugar. Define "a++" such that it is
the same as "a += 1". But what if a is a String? Your program blows
up. So we're back to option 1.

-austin

--
Brian Schröder
http://ruby.brian-schroeder.de/

Austin Ziegler <halostatue@gmail.com> writes:

Okay, the other way is as syntax sugar. Define "a++" such that it is
the same as "a += 1". But what if a is a String? Your program blows
up. So we're back to option 1.

Well, define it as a += a.succ. :slight_smile:

I used to miss ++ very much when I started Ruby, but I can't remember
feeling any need for it in the last time.

···

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Well, that's part of what I forgot to mention; had a scrum meeting
coming up and forgot to finish the email.

Since Ruby's variables don't actually occupy "space" (as objects do),
then #@++ wouldn't do any good in any case, which leaves you with the
syntax sugar. Maybe a combination, if we really wanted this:

  a++ # actually: a = a.@++
  ++a # actually: a = a.@++@

Or something. But that makes it even less acceptable, as the amount of
magic required to do that is very high.

-austin

···

On Thu, 3 Mar 2005 23:34:35 +0900, Brian Schröder <ruby.brian@gmail.com> wrote:

> > Is the "++" syntax used for some other operation, thus precluding
> > its use for a synonym for increment. And wouldn't the right thing
> > for ++ be to increment to the next number?
>
> This comes up every so often, and as much as it has to do with the
> problem of mutating a fixed value (e.g., 5++), it has much more to
> do with the understanding of Ruby variables and the true nature of
> object orientation in Ruby, as well as operator overriding.
>
> At least, IMO.
>
> There's two ways to implement ++. The first way is to implement it
> as an operator, like #+ and #*. Because it is a unary operator, it
> would probably be something like:
>
> def @++
> # implementation
> end
>
> OK. That's fine.

Maybe I didn't understand you, but how would such an implementation
look like? The problem as I see it, is that we are sending messages to
the object and not to the variable. So we can't increment the
variable, because we don't know it.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Austin Ziegler <halostatue@gmail.com> writes:

> Okay, the other way is as syntax sugar. Define "a++" such that it is
> the same as "a += 1". But what if a is a String? Your program blows
> up. So we're back to option 1.

Well, define it as a += a.succ. :slight_smile:

I guess you meant: define it as a = a.succ.

I used to miss ++ very much when I started Ruby, but I can't remember
feeling any need for it in the last time.

Same here. ++, -- and the for loop are gone from my mind when I program
in Ruby, and it's good this way.

Guillaume.

···

On Thu, 2005-03-03 at 23:48 +0900, Christian Neukirchen wrote:

Well, that's part of what I forgot to mention; had a scrum meeting
coming up and forgot to finish the email.

Since Ruby's variables don't actually occupy "space" (as objects do),
then #@++ wouldn't do any good in any case, which leaves you with the
syntax sugar. Maybe a combination, if we really wanted this:

  a++ # actually: a = a.@++
  ++a # actually: a = a.@++@

Or something. But that makes it even less acceptable, as the amount of
magic required to do that is very high.

This may be a dumb question, but what is the meaning of the @ in your
expression?

regards,

Brian

···

--
Brian Schröder
http://ruby.brian-schroeder.de/

Guillaume Marcais <guslist@free.fr> writes:

Austin Ziegler <halostatue@gmail.com> writes:

> Okay, the other way is as syntax sugar. Define "a++" such that it is
> the same as "a += 1". But what if a is a String? Your program blows
> up. So we're back to option 1.

Well, define it as a += a.succ. :slight_smile:

I guess you meant: define it as a = a.succ.

D'oh. Sure.

I used to miss ++ very much when I started Ruby, but I can't remember
feeling any need for it in the last time.

Same here. ++, -- and the for loop are gone from my mind when I program
in Ruby, and it's good this way.

+1

···

On Thu, 2005-03-03 at 23:48 +0900, Christian Neukirchen wrote:

Guillaume.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

This may be a dumb question, but what is the meaning of the @ in your
expression?

uln% ruby -e 'class A; def +@() "unary operator" end end; puts +A.new'
unary operator
uln%

Guy Decoux

> This may be a dumb question, but what is the meaning of the @ in your
> expression?

uln% ruby -e 'class A; def +@() "unary operator" end end; puts +A.new'
unary operator
uln%

Thank you. I searched in the pickaxe 2, but I couldn't find it.

Brian

···

On Fri, 4 Mar 2005 00:48:40 +0900, ts <decoux@moulon.inra.fr> wrote:

Guy Decoux

--
Brian Schröder
http://ruby.brian-schroeder.de/