[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

post inc problem

Sebestyén Gábor

3/3/2005 6:15:00 AM

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



22 Answers

ES

3/3/2005 6:22:00 AM

0

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.

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




Martin DeMello

3/3/2005 7:23:00 AM

0

Sebestyén Gábor <segabor@chello.hu> wrote:
> Hi,
>
> A quick and dumb question: does Ruby (1.6) support postincrementation
> like C++? I mean: " x++ " expression where x is a numeric variable.

I've written up a wiki page on why it doesn't:
http://www.rubygarden.org/ruby?Increme...

martin

Brian Schröder

3/3/2005 9:55:00 AM

0

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

;)

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

hope to help,

Brian

>
> Gábor

--
Brian Schröder
http://ruby.brian-sch...



Randy Kramer

3/3/2005 1:08:00 PM

0

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...

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






WoNáDo

3/3/2005 1:16:00 PM

0

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:9XyVd.561425$6l.37874@pd7tw2no...
> Sebestyén Gábor <segabor@chello.hu> wrote:
> > Hi,
> >
> > A quick and dumb question: does Ruby (1.6) support postincrementation
> > like C++? I mean: " x++ " expression where x is a numeric variable.
>
> I've written up a wiki page on why it doesn't:
> http://www.rubygarden.org/ruby?Increme...
>
> martin

The argumentation in your wiki page is much clear - but - I understood the
"x++", "++x", etc.-stuff as an abbreviation that will be inplemented by some
kind of preprocessing (while compiling or in a macro processor step).



Matthias Luedtke

3/3/2005 1:47:00 PM

0

Martin DeMello wrote:
>>A quick and dumb question: does Ruby (1.6) support postincrementation
>>like C++? I mean: " x++ " expression where x is a numeric variable.
>
>
> I've written up a wiki page on why it doesn't:
> http://www.rubygarden.org/ruby?Increme...

Hi all...

I've just started getting into the ruby flow. While trying out some
statements from your Tutorial I noticed the following:

irb(main):001:0> s = "foo"
=> "foo"
irb(main):002:0> s.id
(irb):2: warning: Object#id will be deprecated; use Object#object_id
=> 24035848

I assume this warning is correct and I would have changed that right
away in the wiki. But because I am new I thought I'd rather ask first
before producing unnecessary work for anyone ;-)

Matthias

Austin Ziegler

3/3/2005 2:14:00 PM

0

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?

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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


Martin DeMello

3/3/2005 2:24:00 PM

0

Matthias Luedtke <matthias-luedtke@gmx.de> wrote:
>
> I've just started getting into the ruby flow. While trying out some
> statements from your Tutorial I noticed the following:
>
> irb(main):001:0> s = "foo"
> => "foo"
> irb(main):002:0> s.id
> (irb):2: warning: Object#id will be deprecated; use Object#object_id
> => 24035848
>
> I assume this warning is correct and I would have changed that right
> away in the wiki. But because I am new I thought I'd rather ask first
> before producing unnecessary work for anyone ;-)

Yes, my mistake :) Please do change it.

martin

Brian Schröder

3/3/2005 2:35:00 PM

0

> > 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-sch...



Christian Neukirchen

3/3/2005 2:49:00 PM

0

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. :-)

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://chneuk...