From: Tobias Cohen on

On 05/11/2009, at 1:31 AM, Marnen Laibow-Koser 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.

It can be done, if you are willing to make your numbers mutable:

class MutableNum
def initialize n
@n = n
end

def pp
@n += 1
@n - 1
end

def method_missing symbol, *args
@n.method(symbol).call(*args)
end

def to_s
@n.to_s
end
end

a = MutableNum.new 1
puts a.pp #=> 1
puts a #=> 2

Having said that, I agree with others that the post-increment operator
is not needed in Ruby at all.

--
Tobias Cohen
http://tobiascohen.com/

From: David A. Black on
Hi --

On Thu, 5 Nov 2009, Tony Arcieri wrote:

> On Wed, Nov 4, 2009 at 2:41 PM, Marnen Laibow-Koser <marnen(a)marnen.org>wrote:
>
>> What semantics do you intend here? I'm not sure I understand.
>>
>
> You can think of it like:
>
> alias_method :++, :increment!

But what would it mean to send the message increment! to, say, the
object 10? In the same vein:

>> 10.succ!
NoMethodError: undefined method `succ!' for 10:Fixnum


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)

From: Aldric Giacomoni on
David A. Black wrote:
>
> But what would it mean to send the message increment! to, say, the
> object 10? In the same vein:
>
> >> 10.succ!
> NoMethodError: undefined method `succ!' for 10:Fixnum

I agree with David.. At which point are we completely violating the
principle of least surprise?
A number is a number is a number. It took me all of half an hour to
forget about ++ and I haven't looked back since. I -like- typing +=1
because it, simply put, makes sense.
--
Posted via http://www.ruby-forum.com/.

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

On Thu, Nov 5, 2009 at 3:40 AM, Gavin Sinclair <gsinclair(a)gmail.com> wrote:

> > def ++
> > incrementing_logic_goes_here
> > end
> >
> > I could see this as being handy
>
> What's wrong with
>
> def inc
> incrementing_logic_goes_here
> end
>
> How is that any different?
>

What's wrong with Array#push? Why do we need Array#<<? How is that any
different?

--
Tony Arcieri
Medioh/Nagravision

From: David A. Black on
Hi--

On Fri, 6 Nov 2009, Tony Arcieri wrote:

> On Thu, Nov 5, 2009 at 3:40 AM, Gavin Sinclair <gsinclair(a)gmail.com> wrote:
>
>>> def ++
>>> incrementing_logic_goes_here
>>> end
>>>
>>> I could see this as being handy
>>
>> What's wrong with
>>
>> def inc
>> incrementing_logic_goes_here
>> end
>>
>> How is that any different?
>>
>
> What's wrong with Array#push? Why do we need Array#<<? How is that any
> different?

irb(main):001:0> [].push(1,2,3)
=> [1, 2, 3]
irb(main):002:0> [].<<(1,2,3)
ArgumentError: wrong number of arguments (3 for 1)

:-) But I know that's not what you meant. The thing is, a method
called ++ that did in-place incremention would not be meaningful for
numbers (if I understand correctly that you mean it would be similar
to succ!), and having it for other objects would probably just lead to
more confusion. That's my hunch, at least.


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)