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

On Sun, Nov 8, 2009 at 11:49 AM, Walton Hoops <walton(a)vyper.hopto.org>wrote:

> How? Because they are immutable? That's not special casing, that's just
> how the class is designed. I can write an immutable class in Ruby,
> without any special casing.
>

Yes, non-Numeric objects can be immutable. However, Numeric objects can't
be mutable.

--
Tony Arcieri
Medioh/Nagravision

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

On Sun, Nov 8, 2009 at 12:20 PM, Seebs <usenet-nospam(a)seebs.net> wrote:

> I don't think that gives the right semantics in many cases. It's also
> not clear that rebinding works:
>
> array_example.length++
>
> What should this do?
>

Well, this is a very interesting question, as I discovered something about
Ruby I didn't know from this...

Say we have:

class Foo
attr_reader :bar

def initialize
@bar = 0
end
end

f = Foo.new
f.bar += 1

What do you think the value of a subsequent call to f.bar will be?

I was surprised to discover that it indeed 1. Somehow += is mutating the
ivar through a supposed "attr_reader" even though there is no corresponding
bar= method. In that case += appears to be frobbing the ivar directly.

Very strange. Even worse:

class Foo
def initialize
@bar = 0
end

def bar
@bar + 1
end
end

f = Foo.new
f.bar += 1

Now what do you think the value of a subsequent call to f.bar will be?

Indeed, it would be 3!

I cannot begin to answer this question because Ruby is doing strange and
unexpected things here, at least from my perspective...

--
Tony Arcieri
Medioh/Nagravision

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

On Sun, Nov 8, 2009 at 12:32 PM, Tony Arcieri <tony(a)medioh.com> wrote:

> I cannot begin to answer this question because Ruby is doing strange and
> unexpected things here, at least from my perspective...
>

Never mind, bar= was still defined because I was reopening the class.

So to answer your question:

array_example.length++

...would attempt to rebind through #length=, and fail if it weren't defined.

--
Tony Arcieri
Medioh/Nagravision

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

On Sun, Nov 8, 2009 at 12:20 PM, Seebs <usenet-nospam(a)seebs.net> wrote:

> And which of those operators special-case Numeric?
>
> Any of them? I don't think so.
>

Admittedly it would be a first.


> If it's to be a rebinding operator, it ought to rebind for everything, not
> just for numerics.


To borrow a phrase from 37signals, "context is more important than
consistency"

--
Tony Arcieri
Medioh/Nagravision

From: Walton Hoops on
> From: bascule(a)gmail.com [mailto:bascule(a)gmail.com] On Behalf Of Tony
> Yes, non-Numeric objects can be immutable. However, Numeric objects
> can't
> be mutable.

Incorrect. In fact the very first message in this thread provided an
example of redifining Fixnum in such a way that it was mutable.