[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

John Pritchard-williams

8/28/2008 9:19:00 PM

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...." :)
--
Posted via http://www.ruby-....

16 Answers

Joel VanderWerf

8/28/2008 9:26:00 PM

0

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

In ruby, operators are methods and they operate on objects, not on
variables.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Robert Dober

8/28/2008 9:44:00 PM

0

On Thu, Aug 28, 2008 at 11:25 PM, Joel VanderWerf
<vjoel@path.berkeley.edu> wrote:
> 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...." :)
>
> In ruby, operators are methods and they operate on objects, not on
> variables.
... and as Integers are immutable ++ cannot change the underlying object.
One could define ++ if one wanted, but it can only work on mutable objects

class MyInteger
def initialize int; @int = int end
define_method "++" do @int += 1 end
def to_s; @int.to_s end
end

m = MyInteger.new 41
puts m
m.send "++"
puts m


Cheers
Robert


--
http://ruby-smalltalk.blo...

There's no one thing that's true. It's all true.
--
Ernest Hemingway

Martin DeMello

8/28/2008 9:48:00 PM

0

On Thu, Aug 28, 2008 at 2:43 PM, Robert Dober <robert.dober@gmail.com> wrote:
> One could define ++ if one wanted, but it can only work on mutable objects
>
> class MyInteger
> def initialize int; @int = int end
> define_method "++" do @int += 1 end
> def to_s; @int.to_s end
> end
>
> m = MyInteger.new 41
> puts m
> m.send "++"
> puts m

Also note that this will not do what you expect from other languages

m = MyInteger.new 41
n = m
puts m
puts n
m.send "++"
puts m
puts n

martin

Mark Firestone

8/28/2008 9:51:00 PM

0

Yes, that really freaked me out the first time it happened to me...
Need n = m.dup

On Thu, Aug 28, 2008 at 10:48 PM, Martin DeMello
<martindemello@gmail.com> wrote:
> Also note that this will not do what you expect from other languages
>
> m = MyInteger.new 41
> n = m
> puts m
> puts n
> m.send "++"
> puts m
> puts n
>
> martin
>
>



--
Peter: I'll handle it, Lois. I read a book about this sort of thing once.

Brian: Are you sure it was a book? Are you sure it wasn't nothing?

Peter: Oh yeah.

John Pritchard-williams

8/28/2008 9:56:00 PM

0


> ... and as Integers are immutable ++ cannot change the underlying object..

That makes sense : and I guess 'x++'=> x+1 would be just confusing..

I mean like this:

a+=1 (which of course IS valid)

a++ - could in theory mean "Take the value pointed at by a currently,
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...

Cheers

John









--
Posted via http://www.ruby-....

David Masover

8/29/2008 5:27:00 AM

0

On Thursday 28 August 2008 16:25:59 Joel VanderWerf wrote:
> John Pritchard-williams wrote:
> > I did check my Ruby books by the way, but they just "unlike C there is
> > no ++ operator in Ruby...." :)
>
> In ruby, operators are methods and they operate on objects, not on
> variables.

That's not really a valid reason, and not entirely true -- there is no +=
method for you to define. It behaves as though it's a macro:

foo += 1
#becomes
foo = foo + 1

I see no reason there couldn't be a ++ operator that behaves the same way:

foo ++
# becomes
foo += 1
# or, probably better:
foo = foo.succ

You can define foo=, and you can define +, but you can't define +=.
Why not add a ++ that works the same way += does?


... Then again, why pollute our namespace with an operator that would see so
little use? I can't remember the last time I had to use "+=1" in Ruby.

Roger Pack

8/29/2008 6:26:00 AM

0

David Masover wrote:
> ... Then again, why pollute our namespace with an operator that would
> see so
> little use? I can't remember the last time I had to use "+=1" in Ruby.

I wish we had it. But that's just me.
-=R
--
Posted via http://www.ruby-....

Robert Dober

8/29/2008 7:05:00 AM

0

On Fri, Aug 29, 2008 at 7:27 AM, David Masover <ninja@slaphack.com> wrote:
> On Thursday 28 August 2008 16:25:59 Joel VanderWerf wrote:
>> John Pritchard-williams wrote:
>> > I did check my Ruby books by the way, but they just "unlike C there is
>> > no ++ operator in Ruby...." :)
>>
>> In ruby, operators are methods and they operate on objects, not on
>> variables.
>
> That's not really a valid reason, and not entirely true -- there is no +=
> method for you to define. It behaves as though it's a macro:
You are right of course, the original question was, why is there no
x++ as syntactic sugar for x = x + 1.
No idea ;).
Cheers
Robert

Christopher Dicely

8/29/2008 7:29:00 AM

0

On Fri, Aug 29, 2008 at 12:04 AM, Robert Dober <robert.dober@gmail.com> wrote:
> On Fri, Aug 29, 2008 at 7:27 AM, David Masover <ninja@slaphack.com> wrote:
>> On Thursday 28 August 2008 16:25:59 Joel VanderWerf wrote:
>>> John Pritchard-williams wrote:
>>> > I did check my Ruby books by the way, but they just "unlike C there is
>>> > no ++ operator in Ruby...." :)
>>>
>>> In ruby, operators are methods and they operate on objects, not on
>>> variables.
>>
>> That's not really a valid reason, and not entirely true -- there is no +=
>> method for you to define. It behaves as though it's a macro:
> You are right of course, the original question was, why is there no
> x++ as syntactic sugar for x = x + 1.

Because Ruby is hard enough to parse as it is. After all, ++x is
already valid Ruby. equivalent to x.send(:"+@").send(:"+@"). And x++x
is valid (though odd) Ruby, equivalent to x.+(x.send(:"+@")).

Its probably better to just let people use x+=1.

Lars Christensen

8/29/2008 8:14:00 AM

0

On Aug 28, 11:43 pm, Robert Dober <robert.do...@gmail.com> wrote:
> On Thu, Aug 28, 2008 at 11:25 PM, Joel VanderWerf<vj...@path.berkeley.edu> wrote:
> > John Pritchard-williams wrote:
>
> >> There's must be very good simple reason why there is no 'x++' method
> >> Ruby right?
>
> >> I did check my Ruby books by the way, but they just "unlike C there is
> >> no ++ operator in Ruby...." :)
>
> > In ruby, operators are methods and they operate on objects, not on
> > variables.
>
> .. and as Integers are immutable ++ cannot change the underlying object.
> One could define ++ if one wanted, but it can only work on mutable objects

I think the OP deserves the fuller explanation; Why are integers
immutable? Consider this:

a = 2
b = a
a++

Since everything is an object, 'a' references the object '2' and b
should reference it too after the assignment. Then 'a++' should
increment the object referenced by both a and b? This would of course
be possible, but it would be a rather huge performance penalty to keep
something as simple as integers as references to objects everywhere.

Ruby keeps 'small-enough' integers in the object reference (pointer)
to improve performance. Hence 'b = a' results in an actual copy being
taken of the value '2'. If you increment 'b', 'a' would retain its old
value, breaking the concept that a variable is always just a
reference. Ruby opts for immutable Integers instead.

Lars