From: Tony Arcieri on
[Note: parts of this message were removed to make it a legal post.]

On Wed, Nov 4, 2009 at 10:23 AM, Martin DeMello <martindemello(a)gmail.com>wrote:

> And how exactly would you change the value of 1 in place?
>

Another way to look at it: does Fixnum#+ change the value of its receiver?

--
Tony Arcieri
Medioh/Nagravision

From: Paul Smith on
On Wed, Nov 4, 2009 at 6:16 PM, Tony Arcieri <tony(a)medioh.com> wrote:
> On Wed, Nov 4, 2009 at 10:23 AM, Martin DeMello <martindemello(a)gmail.com>wrote:
>
>> And how exactly would you change the value of 1 in place?
>>
>
> You don't.  Are you insinuating the behavior of Fixnums isn't already
> special cased to begin with?

a = 1

a.class #Fixnum

a++ # a is now 2

1.class #Fixnum

1++ # Illegal

So although a is a Fixnum, and 1 is a Fixnum, they respond to ++ differently?

>
> --
> Tony Arcieri
> Medioh/Nagravision
>



--
Paul Smith
http://www.nomadicfun.co.uk

paul(a)pollyandpaul.co.uk

From: RichardOnRails on
On Nov 4, 9:37 am, Seebs <usenet-nos...(a)seebs.net> wrote:
> On 2009-11-04, Marnen Laibow-Koser <mar...(a)marnen.org> wrote:
>
> > I believe you are quite wrong.  If a destructive function like gsub! can
> > be implemented as a method, then I see no reason that +=, |=, or postfix
> > ++ couldn't be.
>
> gsub! is implemented as a method on objects which contain data.  ++ would
> have to be implemented as a method on objects which ARE their data -- which
> have no distinction between the object and its "contents".
>
> gsub! can work because somewhere inside the object there is a hunk of storage
> which is separate from the object itself.  Fixnum has no such storage to
> refer to.
>
> -s
> --
> Copyright 2009, all wrongs reversed.  Peter Seebach / usenet-nos...(a)seebs.nethttp://www.seebs.net/log/<-- lawsuits, religion, and funny pictureshttp://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


> ++ would have to be implemented as a method on objects which ARE their data

Not true. Check out the following, step by step:

def show(v)
"Got #{v}, class = #{v.class}, object_id = #{v.object_id}
(v.object_id-1)/2 = #{(v.object_id-1)/2 }"
end

class Fixnum
def pp # We can’t define ++ because of a compiler restriction.
self + 1
end
end

These lines show that a & b values are stored in there object_ids held
in the symbol table.
Don’t believe it? Read more.
a = 1; show (a) => Got 1; class = Fixnum; object_id = 3; v >> 1
= 1
b = 1; show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1
= 1
a == b => true

Appending these lines shows that a & b values are distinct.
That is, after incrementing, a =2, b is unchanged; b is not
impacted by a’s change
a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1
= 2
show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1

Appending these lines shows the ++’s alias pp works just find
a=1; show(a.pp) => Got 2; class = Fixnum; object_id = 5; v >> 1
= 2
show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1

Appending these lines show that ++ crosses the Fixnum/Bignum boundary
a = 2**30-1; show (a) => Got 1073741823; class = Fixnum; object_id
= 2147483647; v >> 1 = 1073741823
show(a.pp) => Got 1073741824; class = Bignum; object_id =
22738520; v >> 1 = 11369260 # “v >> 1” is irrelevant, of course.

Do you agree?

Best wishes,
Richard

From: Walton Hoops on
> -----Original Message-----
> From: bascule(a)gmail.com [mailto:bascule(a)gmail.com] On Behalf Of Tony
> Arcieri
> wrote:
> I think you're missing why ++ could be useful, and it's precisely
> because
> Ruby is a "21st century language"
>
> The ++ operator, far more than just being syntactic sugar for +=1,
> would
> allow you to send an "increment" message to any object, which would
> change
> its value in place, i.e.
>
> def ++
> incrementing_logic_goes_here
> end
>
> I could see this as being handy

But you already can with the mechanics of the language that are already
present!

irb(main):003:0> i=15
=> 15
irb(main):004:0> i=i.succ
=> 16
irb(main):005:0> i="15"
=> "15"
irb(main):006:0> i=i.succ
=> "16"
irb(main):007:0> i=1.2
=> 1.2
irb(main):008:0> i=i.succ
NoMethodError: undefined method `succ' for 1.2:Float
from (irb):8
from /usr/local/bin/irb:12:in `<main>'

In an object that it makes sense to increment, define the #succ method!
It's that easy!


From: Seebs on
On 2009-11-04, RichardOnRails <RichardDummyMailbox58407(a)USComputerGurus.com> wrote:
> class Fixnum
> def pp # We can?t define ++ because of a compiler restriction.
> self + 1
> end
> end

This doesn't seem to do the right thing.

a = 1
a.pp

Is a now 2? If not, you haven't implemented an increment operator.

> Appending these lines shows that a & b values are distinct.
> That is, after incrementing, a =2, b is unchanged; b is not
> impacted by a?s change
> a += 1; show (a) => Got 2; class = Fixnum; object_id = 5; v >> 1
>= 2
> show (b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1

Right. You've changed which object a refers to, because you've reassigned
a.

> Appending these lines shows the ++?s alias pp works just find
> a=1; show(a.pp) => Got 2; class = Fixnum; object_id = 5; v >> 1
>= 2
> show(b) => Got 1; class = Fixnum; object_id = 3; v >> 1 = 1

Not the same. The key is that, after "a += 1", not only do you get 2,
but a is now 2.

> Do you agree?

No.

For "a.pp" to be the same as a++ in other languages, you'd have to do:

a = 1; a.pp; show(a) => Got 2



If you don't get a "2" by using a.pp, it's not an increment, just a "one
more than".

Consider a loop:

a = 1
while ((a += 1) < 10) do
puts a
end

Now, try:

a = 1
while (a.pp < 10) do
puts a
end

Doesn't do the same thing.

-s
--
Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam(a)seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!