From: David A. Black on
Hi --

On Sat, 7 Nov 2009, Seebs wrote:

> On 2009-11-06, David A. Black <dblack(a)rubypal.com> wrote:
>> It seems like a lot of special-casing and strangeness, though. I'm a
>> little bit hampered in discussing it, I guess, because I don't see
>> what benefit it would confer in exchange for the anomaly. So I'm
>> probably going in circles.
>
> I have found a lot of idioms which are amenable to use with ++, especially
> postincrement, but they are often not nearly so useful outside of the C-like
> languages.
>
> Consider the canonical inner loop for copying an array in C:
> s[i++] = t[j++];
>
> There's really no idiomatic equivalent -- nor a need for one, usually.

Oh, I have no problem with ++ per se. It just seems against the grain
in Ruby.


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: Michael W. Ryder on
Walton Hoops wrote:
>> -----Original Message-----
>> From: marnen(a)marnen.org [mailto:marnen(a)marnen.org]
>>>> 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!

But if you wanted to do something like:
i = 10;
while (i > 0)
{
printf("%d/n", i--);
}
in Ruby you would have to do something like:
i = 10
while (i > 0)
puts i
i -= 1
end
As far as I can tell there is no way in Ruby to use .each or .times to
go backwards. While I realize this thread is about the ++ operator the
-- operator is complementary.

<snip>
From: Marnen Laibow-Koser on
Michael W. Ryder wrote:
[...]
> But if you wanted to do something like:
> i = 10;
> while (i > 0)
> {
> printf("%d/n", i--);
> }
> in Ruby you would have to do something like:
> i = 10
> while (i > 0)
> puts i
> i -= 1
> end

No.

10.downto(1) do |i|
puts i
end

> As far as I can tell there is no way in Ruby to use .each or .times to
> go backwards.

That's what .downto is for. (Have you ever needed this? I have not.)

> While I realize this thread is about the ++ operator the
> -- operator is complementary.
>
> <snip>

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

From: Matthew K. Williams on
On Sat, 7 Nov 2009, Michael W. Ryder wrote:

> Walton Hoops wrote:
>>> -----Original Message-----
>>> From: marnen(a)marnen.org [mailto:marnen(a)marnen.org]
>>>>> 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!
>
> But if you wanted to do something like:
> i = 10;
> while (i > 0)
> {
> printf("%d/n", i--);
> }
> in Ruby you would have to do something like:
> i = 10
> while (i > 0)
> puts i
> i -= 1
> end
> As far as I can tell there is no way in Ruby to use .each or .times to go
> backwards. While I realize this thread is about the ++ operator the --
> operator is complementary.
>

what about the downto method?

Matt

From: David A. Black on
Hi --

On Sat, 7 Nov 2009, Marnen Laibow-Koser wrote:

> Michael W. Ryder wrote:
> [...]
>> But if you wanted to do something like:
>> i = 10;
>> while (i > 0)
>> {
>> printf("%d/n", i--);
>> }
>> in Ruby you would have to do something like:
>> i = 10
>> while (i > 0)
>> puts i
>> i -= 1
>> end
>
> No.
>
> 10.downto(1) do |i|
> puts i
> end
>
>> As far as I can tell there is no way in Ruby to use .each or .times to
>> go backwards.
>
> That's what .downto is for. (Have you ever needed this? I have not.)

And in 1.9:

puts *10.downto(1)

and there's also #reverse_each.


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)