[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

for or each?

tekwiz

9/20/2008 7:50:00 PM

I just used the new roodi gem to check out some of my code that has a
lot of algorithmic code. It gave me a number of issues with the
phrase "Don't use 'for' loops. Use Enumerable.each instead." I prefer
for loops as opposed to using each simply because it's what I'm used
to coming from C-style languages.

Example:

This is what I do:

for i in 0...str.size
...
end

This is what roodi would have me do

(0...str.size).each do |i|
...
end

Is there a real, substantive reason to use each instead of for? Or is
it simply just a preference issue?

Thanks,
--
TekWiz

35 Answers

Phlip

9/20/2008 8:35:00 PM

0

tekwiz wrote:

> This is what roodi would have me do
>
> (0...str.size).each do |i|
> ...
> end
>
> Is there a real, substantive reason to use each instead of for? Or is
> it simply just a preference issue?

It leaves you closer to a refactor to .map or .inject or .select or .reject or
..delete_if or .each_index or .each_with_index or ...

--
Phlip
http://www.oreillynet.com/ruby/blog/2008/09/polygamous_assert_l...

Phlip

9/20/2008 8:37:00 PM

0

Phlip wrote:
> tekwiz wrote:
>
>> This is what roodi would have me do
>>
>> (0...str.size).each do |i|

> It leaves you closer to a refactor to .map or .inject or .select or
> .reject or .delete_if or .each_index or .each_with_index or ...

It also hints at:

str.each do |ch|

....

Sebastian Hungerecker

9/20/2008 8:44:00 PM

0

Phlip wrote:
> str.each do |ch|

That'd be each_char, I suppose. String#each is each_line.

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Phlip

9/20/2008 9:02:00 PM

0

>> str.each do |ch|
>
> That'd be each_char, I suppose. String#each is each_line.

IOW:

... or .each_char or .each_line or .each_byte or ...

tekwiz

9/20/2008 9:07:00 PM

0

On Sep 20, 3:32=A0pm, Phlip <phlip2...@gmail.com> wrote:
> Phlip wrote:
> > tekwiz wrote:
>
> >> This is what roodi would have me do
>
> >> =A0 (0...str.size).each do |i|
> > It leaves you closer to a refactor to .map or .inject or .select or
> > .reject or .delete_if or .each_index or .each_with_index or ...
>
> It also hints at:
>
> =A0 =A0str.each do |ch|

So, it's a code-readability issue and not a functional or complexity
issue?

--
TekWiz

Sebastian Hungerecker

9/20/2008 9:10:00 PM

0

Phlip wrote:
> > That'd be each_char, I suppose. String#each is each_line.
>
> IOW:
>
> =A0 ... or .each_char or .each_line or .each_byte or ...

What?

Confused,
Sebastian
=2D-=20
Jabber: sepp2k@jabber.org
ICQ: 205544826

Phlip

9/20/2008 10:01:00 PM

0

>> ... or .each_char or .each_line or .each_byte or ...
>
> What?

I illustrated that you augmented the "or" list from my first post.

Joe Wölfel

9/20/2008 10:05:00 PM

0

It's interesting that array access using 'each' seems to be much
faster on my machine. In C, indexed-based for-loops are slow. It's
faster to increment pointers. Maybe it's similar under Ruby's hood.

ruby 1.86 (OS X PPC)
Rehearsal --------------------------------------------------
for loop 1.200000 0.000000 1.200000 ( 1.206533)
each 0.510000 0.000000 0.510000 ( 0.511994)
----------------------------------------- total: 1.710000sec

user system total real
for loop 1.190000 0.000000 1.190000 ( 1.190023)
each 0.500000 0.000000 0.500000 ( 0.508636)


ruby 1.9 (Same OS X PPC)
Rehearsal --------------------------------------------------
for loop 2.370000 0.010000 2.380000 ( 2.376402)
each 1.770000 0.000000 1.770000 ( 1.775798)
----------------------------------------- total: 4.150000sec

user system total real
for loop 2.310000 0.010000 2.320000 ( 2.316495)
each 1.780000 0.000000 1.780000 ( 1.775958)

Joe

On 20 sept. 08, at 15:50, tekwiz wrote:

> I just used the new roodi gem to check out some of my code that has a
> lot of algorithmic code. It gave me a number of issues with the
> phrase "Don't use 'for' loops. Use Enumerable.each instead." I prefer
> for loops as opposed to using each simply because it's what I'm used
> to coming from C-style languages.
>
> Example:
>
> This is what I do:
>
> for i in 0...str.size
> ...
> end
>
> This is what roodi would have me do
>
> (0...str.size).each do |i|
> ...
> end
>
> Is there a real, substantive reason to use each instead of for? Or is
> it simply just a preference issue?
>
> Thanks,
> --
> TekWiz
>


Phlip

9/20/2008 10:17:00 PM

0

tekwiz wrote:

>>> It leaves you closer to a refactor to .map or .inject or .select or
>>> .reject or .delete_if or .each_index or .each_with_index or ...

> So, it's a code-readability issue and not a functional or complexity
> issue?

'for' is arguably more readable. And it's not a performance issue - I suspect
the opcodes will be the same. It is very much a technical issue.

Good code is readable, minimal, and maintainable. Maintaining code requires
adding new features. Code should always be as ready for change as possible, so
much of our design rules (such as "object orientation") are really rubrics for
improving the odds that the next change comes easy.

This is an easier change...

array.each{|x| ... } -> array.map{|x| ... }

....than this:

for x in array ... -> array.map{|x| ... }

Further, your original example was very C-like. The iteration variable was the
array's index. Most iteration directly addresses each array's element, without
regard to its index. So 'for i in 0...str.size' is often more excessive than
'for x in str'.

Using .each leads to the correct mindset. Put another way, 'for' is an obsolete
concept - a legacy of languages without true iteration.

--
Phlip

Xavier Noria

9/20/2008 10:22:00 PM

0

I don't think the minimal editing distance between #each and #map and
friends has anything to do. It is so unlikely that an #each becomes an
#inject that I don't think that's a good explanation for why people
prefer it over for. If <<some code>> becomes an #inject you just go an
edit whatever you need. Nah I don't think so.

The analogous for loop is written like this

for item in collection
# do something with item
end

I think #each has become the common for-idiom in Ruby because, you
know, the community has converged to that choice by themselves. That's
not very tangible, and perhaps may be due to the fact that blocks are
ubiquitous in Ruby and #each is syntactically closer in your head to a
lot of other stuff in Ruby.

I'd say #each is not that much favoured in ERb templates.