From: Marnen Laibow-Koser on
Tony Arcieri wrote:
> On Wed, Nov 4, 2009 at 1:46 PM, Marnen Laibow-Koser
> <marnen(a)marnen.org>wrote:
>
>> This is unidiomatic Ruby. In fact, it's becoming clear to me that just
>> about *any* use case for postfix ++ is unidiomatic Ruby. The cleanest
>> way *in Ruby* of doing what you did would be
>>
>> (1..10).each {|i| puts i}
>>
>> Incrementing is handled under the hood.
>>
>
> What if it's not?
>
> m = CounterInMemcacheOrSomething.new :foobar
> m++

What semantics do you intend here? I'm not sure I understand.

In any case, you have several perfectly good alternatives:

* use CounterInMemcacheOrSomething.all(:foobar).each{|m| ...}

* define CounterInMemcacheOrSomething#succ! if it's mutable

* or even use m += 1

++ is very useful in C-like languages and PHP. I thought it would be
useful in Ruby too, but I now believe that it wouldn't, since it's only
really good for stepping through structures -- and Ruby handles that
better with iterators.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: Walton Hoops on
> -----Original Message-----
> From: Michael W. Ryder [mailto:_mwryder55(a)gmail.com]
> But i.succ does Not work in the following:
>
> i = 1
> while (i < 10)
> puts i.succ
> end
>
> the only way to get this to work is to use:
> puts i; i = i.succ
>
> which is not as clean as using puts i++.

I'd argue it's much, much cleaner. ++ has long been a source
of confusion for C programmers. Consider:

int i = 1;
while (i < 10)
{
printf("%d,",i++);
}

What does it ouput? 1,2,3,4,5,6,7,8,9, or 2,3,4,5,6,7,8,9,10,?

Because we're all experienced C programmers here we of course know
it to be 1,2,3,4,5,6,7,8,9, but it's not uncommon for experienced C
programmers to make mistakes around a++ vs ++a. On the other hand:

int i = 1;
while (i <10)
{
printf("%d,",i);
i+=1;
}

Makes it explicitly clear what is happening. Then consider the ruby
direct ruby translation:

i = 1;
while (i <10)
print "#{i},"
i+=1;
end

Even a non-programmer is going to have a pretty darn clear idea of
what is going on here. Sure it's one line longer but much more
understandable, and therefore cleaner. If there is one thing
playing Perl Golf should have taught all programmers it's that
shorter != better.

Now consider the ruby way:

10.times do |i|
print "#{i},"
end

Some length as the C code, but much more readable. Heck, it's
almost English! Which is part of the beauty of Ruby: It's simple,
natural, readable syntax. I've seen a lot of arguments that it
doesn't fit with ruby's object model, but to me that's not the key
point. ++ doesn't fit with Ruby's elegant syntax.


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

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!

--
Tony Arcieri
Medioh/Nagravision

From: Yukihiro Matsumoto on
Hi,

In message "Re: Ruby doesn't implement x++ for Fixnum's because ???"
on Thu, 5 Nov 2009 04:25:05 +0900, RichardOnRails <RichardDummyMailbox58407(a)USComputerGurus.com> writes:

|BTW, I'm not advocating x++ for Ruby. I'm just trying to understand
|whether Ruby would literally change 1 to 2 as opposed to change a
|variable that contains 1 to subsequently contain 2.

There's no way to modify local variables by sending message in Ruby.

matz.

From: Michael W. Ryder on
Walton Hoops wrote:
<snip>

> Now consider the ruby way:
>
> 10.times do |i|
> print "#{i},"
> end
>
> Some length as the C code, but much more readable. Heck, it's
> almost English!

Not for me it wasn't. I had to try it to see that it actually works.
My initial impression was that it would print 10 copies of i. I still
don't see where 'i' is incremented so this is one of those "magical"
constructs much like your impression of ++ in C. I would find this much
harder to maintain than the C version.

Which is part of the beauty of Ruby: It's simple,
> natural, readable syntax. I've seen a lot of arguments that it
> doesn't fit with ruby's object model, but to me that's not the key
> point. ++ doesn't fit with Ruby's elegant syntax.
>
>