From: Stu Date: 2011-07-28T05:54:58+09:00 Subject: Re: How do we read this line of code? Hello abder.rahman.ali each is an important part of ruby and it is something people from other programming languages tend to find a bit confusing at first. It is basically a foreach loop( i.e. a loop that defeats the possibility of a off-by-one index based bug/error.) It is also a common design pattern. It should be read as: traverse each item in collection @collection.each do |item| item is local to the anonymous block passed to each. the sigil @ represents a instance variable global to a singular birthed class only (i.e. the variables set with the sigil are accessed outside of method scope as well as within but not outside the class instance itself( this is in contrast to the double at sigil which acts as a global variable for every instance birthed from the same class template.) ) an example usage of each on an array would be: @lost = [4,8,15,16,23,42] lost_devided = Array.new @lost.each {|item| lost_devided << item / 2} lost_devided [2, 4, 7, 8, 11, 21] do/end are synonymous to curly brackets btw. The final note about each. It is redesigned( overloaded) in many classes to work with a specific task of collections to aid the programmers with a common interface. If you ever create a class which acts as a collection it is common to redefine each to work within that class as well as other rubyisms such as to_s and so on. The more common the paradigm/syntax; the more fluid your mental and programmatic flow will work within the creation of your own programs. It also allows easy adoption( and adaption) for others to use your libraries. ~Stu On Wed, Jul 27, 2011 at 1:24 PM, SW Engineer wrote: > If we have the following line of Ruby code: > > @x.each do |x| > > - What does |x| represent here? > - How do we read the line (i.e; in English)? > > Thanks. > > -- > Posted via http://www.ruby-forum.com/. > >