2

I have some doubts about the diference beetwen 'for' and 'each' Iterators on Ruby on rails.

I have this case, how is the best practice for this?

Def(method) present in the application_helper.rb file:

  def milestone_icons
    icons = ["", "icon-world", "icon-star-twohdd-o", "icon-users", "icon-notification", "icon-clock", "icon-eye", "icon-like-filled", "icon-credit-card", "icon-bell" ]
end

Select present in the haml file:

%select.js-select-icons{style: 'width: 50px', name: 'milestone[icon]'}
  - for icon in milestone_icons do
      %option{value: icon, type: "icon", selected: (@generic.icon == icon)}
        = "<i class='#{icon}'></i>".html_safe
Pang
  • 9,564
  • 146
  • 81
  • 122
Andre Leoni
  • 204
  • 1
  • 11
  • the way you're using it here (`for icon in milestone_icons`) is the same as writing `milestrone_icons.each do |icon|`. – max pleaner Oct 03 '16 at 02:00
  • Ok, thanks, however, for ruby, have difference? Have a best practice for this type of condition? – Andre Leoni Oct 03 '16 at 02:10
  • 2
    Enumerators are often preferred to loops (including `for` loops) because with the former the value of the iterator is confined to the block, wheres it is for all to see when using a loop. For example: `for n in [1,2,3] do; puts n; end; n #=> 3` (after printing `"1"`, `"2"`, `"3"`). By contrast, `[1,2,3].each { |n| puts n }; n #=> NameError: undefined local variable or method 'n' for main:Object`. Many (most?) Rubiests *never* use `for` loops. I've *never* used one, not even once! :-) – Cary Swoveland Oct 03 '16 at 02:15
  • 1
    Thanks for the reply, I never will use the 'for' again too! xD – Andre Leoni Oct 03 '16 at 02:23

1 Answers1

3

Surprisingly the for construct in Ruby is hardly ever used. People tend to prefer the each style iterator or one of its friends like each_with_index or each_with_object as those are significantly more versatile and often perform better.

I'd strongly recommend you use the each method unless you have a very compelling reason.

The differences are somewhat academic, but the big advantage of using each is you're actually interacting with an iterator and you have a lot of ways of using that. You can actually pass through an iterator from one method to another, and you can chain them together to set up the right structure before doing any work.

The real meat here is inside Enumerable where each is the most basic version of this kind of iterator. There are many that are often a much better fit for whatever problem you're trying to solve.

So, yes, for and each are basically the same, but each is better because it's got friends.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Thanks for the answer! Now i understand, it is not recommended because of performance or other things, but is because the family of object that i can use with them... In this case, I will use the 'each' now, and if i have other doubt, i ask here in the stackoverflow. – Andre Leoni Oct 03 '16 at 02:21
  • I believe the`for` loop is in fact built using the `each` iterator. – Sagar Pandya Oct 03 '16 at 04:57
  • @sagarpandya82 I'm not so sure about that. There's subtle differences in how they work, even if they're largely the same. – tadman Oct 03 '16 at 17:10
  • 1
    I read this in the book `Eloquent Ruby` but it omitted how this works. I've posted a [question here](http://stackoverflow.com/q/39839809/5101493) and contacted the author of the book to post an answer (which he kindly has). I hope this is of interest. – Sagar Pandya Oct 03 '16 at 20:37
  • @sagarpandya82 That's some interesting exposition, thanks! – tadman Oct 03 '16 at 20:50