How do we read this line of code?

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

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

A block variable

- How do we read the line (i.e; in English)?

@x.each do |x|. :stuck_out_tongue:

"For each item in @x, called x, and do..."

···

On Wed, Jul 27, 2011 at 8:24 PM, SW Engineer <abder.rahman.ali@gmail.com> wrote:

--
Phillip Gawlowski

phgaw.posterous.com | twitter.com/phgaw | gplus.to/phgaw

A method of solution is perfect if we can forsee from the start,
and even prove, that following that method we shall attain our aim.
-- Leibniz

SW Engineer wrote in post #1013421:

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

>x> represents each item found within @x...

- How do we read the line (i.e; in English)?

if @x is an array, you could read this as "for each entry (x) in the
array (@x,) *do* something nifty..."

-j

···

--
Posted via http://www.ruby-forum.com/\.

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

It's an argument supplied to the block that follows. In this case, the
argument is an element of @x passed to the block.

- How do we read the line (i.e; in English)?

I'd probably read it as something like:

    For EACH element X of INSTANCE VARIABLE X, DO . . .

. . . if I was trying to make it read as smoothly as possible in English
without giving much in the way of specific clues about Ruby syntax,
sticking to the semantics of it. Note that I emphasized syntactic
elements that actually appear in the code in my example by capitalizing
all the letters in each of these elements.

···

On Thu, Jul 28, 2011 at 03:24:08AM +0900, SW Engineer wrote:

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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 <abder.rahman.ali@gmail.com> 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/\.

SW Engineer wrote in post #1013421:

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

@x = [10, 20, 30]

@x.each do |x|
  puts x
end

--output:--
10
20
30

- How do we read the line (i.e; in English)?

"At-x dot each do x"

···

--
Posted via http://www.ruby-forum.com/\.

It is not an instance variable.

···

On Jul 27, 2011, at 11:53 , Chad Perrin wrote:

On Thu, Jul 28, 2011 at 03:24:08AM +0900, SW Engineer wrote:

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

It's an argument supplied to the block that follows. In this case, the
argument is an element of @x passed to the block.

- How do we read the line (i.e; in English)?

I'd probably read it as something like:

   For EACH element X of INSTANCE VARIABLE X, DO . . .

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

Careful: we have no other context information and we do not know what
@x actually points to. So we can only say something like

Invoke the block for each item which method @x.each yields.

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.

But then again, it's not hard to create a class which follows the
Enumerable contract either:

class Silly
  include Enumerable

  def each
    rand(47).times { yield rand(11) }
    self
  end
end

Now with

@x = Silly.new

we can do

@x.each do |x| puts x end

Kind regards

robert

···

On Wed, Jul 27, 2011 at 10:54 PM, Stu <stu@rubyprogrammer.net> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Since when is @x not an instance variable?

···

On Thu, Jul 28, 2011 at 05:19:58AM +0900, Ryan Davis wrote:

On Jul 27, 2011, at 11:53 , Chad Perrin wrote:

> On Thu, Jul 28, 2011 at 03:24:08AM +0900, SW Engineer wrote:
>> If we have the following line of Ruby code:
>>
>> @x.each do |x|
>>
>> - What does |x| represent here?
>
> It's an argument supplied to the block that follows. In this case, the
> argument is an element of @x passed to the block.
>
>
>> - How do we read the line (i.e; in English)?
>
> I'd probably read it as something like:
>
> For EACH element X of INSTANCE VARIABLE X, DO . . .

It is not an instance variable.

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

Ah. I was pre-caffiene and thought you were referring to the block variable.

···

On Jul 27, 2011, at 14:29 , Chad Perrin wrote:

On Thu, Jul 28, 2011 at 05:19:58AM +0900, Ryan Davis wrote:

On Jul 27, 2011, at 11:53 , Chad Perrin wrote:

On Thu, Jul 28, 2011 at 03:24:08AM +0900, SW Engineer wrote:

If we have the following line of Ruby code:

@x.each do |x|

- What does |x| represent here?

It's an argument supplied to the block that follows. In this case, the
argument is an element of @x passed to the block.

- How do we read the line (i.e; in English)?

I'd probably read it as something like:

  For EACH element X of INSTANCE VARIABLE X, DO . . .

It is not an instance variable.

Since when is @x not an instance variable?