From: Seebs on
On 2009-11-06, David A. Black <dblack(a)rubypal.com> wrote:
> Oh, I have no problem with ++ per se. It just seems against the grain
> in Ruby.

Yeah. I think fundamentally it's the "variables aren't objects" thing.
++ is intended to operate on a specific object, and doesn't make sense
if "x = x + 1" doesn't really mean "modify the specific object x".

-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!
From: Michael W. Ryder on
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.)
>

I missed the downto method, I guess that is a problem when you have so
many different ways to do the same basic things. I much prefer the
simplicity of Basic and C with for loops that can go either direction.
As far as going backwards I use it a lot to parse strings of the form
"city name ST 12345-6789" to City, State, and Zip Code fields. I look
for the first blank from the end of the string and assume everything
after it is the Zip Code, I then find the next two non-blank characters
and assign them to State, and everything else is the City name.

>> 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
From: David A. Black on
Hi --

On Sat, 7 Nov 2009, Michael W. Ryder wrote:

> 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.)
>>
>
> I missed the downto method, I guess that is a problem when you have so many
> different ways to do the same basic things. I much prefer the simplicity of
> Basic and C with for loops that can go either direction. As far as going
> backwards I use it a lot to parse strings of the form "city name ST
> 12345-6789" to City, State, and Zip Code fields. I look for the first blank
> from the end of the string and assume everything after it is the Zip Code, I
> then find the next two non-blank characters and assign them to State, and
> everything else is the City name.

I think one thing that's getting lost in the sauce here is that Ruby
does have idioms like:

a -= 1

for bumping things up and down and other operations. So you can
maintain a manual index on a collection or string traversal easily if
you need to. I'd say that most of the time, though, you won't need to.


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: Marnen Laibow-Koser on
Michael W. Ryder wrote:
[...]
>. I much prefer the
> simplicity of Basic and C with for loops that can go either direction.

That's because you're trying to write C in Ruby. There are far more
idiomatic ways of doing things -- and they *are* clearer, at least in a
Ruby context.

> As far as going backwards I use it a lot to parse strings of the form
> "city name ST 12345-6789" to City, State, and Zip Code fields. I look
> for the first blank from the end of the string and assume everything
> after it is the Zip Code, I then find the next two non-blank characters
> and assign them to State, and everything else is the City name.

That's great in a language like C that doesn't have very good string
handling. The Ruby way to do this would be
city, state, zip = string.split(/\s+/)

No incrementing. No iteration. Just a clear declarative syntax.

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 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.

Others have mentioned reverse each and downto, so I'll just throw in one
more. If you are determined to save that line, you also have:
(untested, so if I'm off my one, from the C feel free to tar and feather me)

i=11
while (i-=1) > 0
puts i
end

or:

i=11;
while i > 0
puts (i-=1)
end

I also think this demonstrates my previous point, that playing Perl golf
serves no one. I've found it rare that saving a line at the cost of
readability improves code in any way. If the interpreter is any good,
your 5 line example should execute just as fast as my 4 line, and it's
certainly much clearer what is actually being done. I've seen plenty of
C programmers who really should know better, get confused by ++ in
unexpected places.


What it all comes down to in my personal opinion (as no one of any real note)
is that while ++ can be a cool little operator, it really doesn't give enough
benefit to be worth the confusion involved in implementing it in Ruby. It's
been made plenty clear by others why ++ would have to be a special case operator
in Ruby (as opposed to the other operators which are simple methods), but why
write special logic for this one silly operator that at BEST saves us one line
of code here and there?