Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the point of "for x in y" in Ruby?

I'm learning Ruby and RoR at the moment, and I came across this:

<% for post in @posts %>

in the Rails guide. I'd understood that the idiomatic way to do this in Ruby is with:

<% @posts.each do |post| %>

If there is a difference then what is it? And if there isn't a difference then wouldn't it be better for the Rails people to be pushing proper Ruby idioms (rather than this, which looks more pythonic to me)?

Edit: I've just found two conflicting explanations of this: Tutorials Point says they're the same except "a for loop doesn't create a new scope for local variables", whereas CS.Auckland.ac.NZ says for is just syntactic sugar for the equivalent .each.

Edit2: The for ... in in question was for the index.html.erb generated in app/views/posts by script/generate scaffold. I've done a quick check and that now generates the .each syntax. I guess that part of the guide was written at an earlier stage of rails development when the scaffold generated for ... in.

Edit3: I can now confirm that for x in y was used in Rails 2.2.2, but by 2.3.8 it is using y.each do |x|. Just so you know.

like image 771
Skilldrick Avatar asked Jul 27 '10 10:07

Skilldrick


People also ask

What is Ruby and why should you learn it?

He created Ruby to combine what he thought were the best features of these programming languages. Ruby is a very flexible programming language that allows developers to alter how the language itself works. You can add functionality to core language features or even remove them if you need. Ruby is also a highly portable, cross-platform language.

What is the use of exponent and operator in Ruby?

Exponent AND assignment operator, performs exponential (power) calculation on operators and assign value to the left operand. Ruby also supports the parallel assignment of variables. This enables multiple variables to be initialized with a single line of Ruby code. For example − This may be more quickly declared using parallel assignment −

What are floating-point numbers in Ruby?

The floating-point numbers are object of class Float. Note: Underscore can be used to separate a thousand places e.g: 25_120.55 is the same as the number 25120.55. Example 1: Basic arithmetic operations on numbers in Ruby is shown below.

How do you interpret a +B in Ruby?

a + b is interpreted as a+b ( Here a is a local variable) a +b is interpreted as a (+b) ( Here a is a method call) Ruby interprets semicolons and newline characters as the ending of a statement. However, if Ruby encounters operators, such as +, −, or backslash at the end of a line, they indicate the continuation of a statement.


1 Answers

The tutorialpoint page is correct, for is equivalent to each except for the scoping difference. Here's a demonstration:

arr = [1,2,3]
arr.each do |x|
  last = x
end
last # NameError

vs.

arr = [1,2,3]
for x in arr
  last = x
end
last #=> 3

If you want to make it work using each, you need to do last = nil before the loop. This is, as the link pointed out, because blocks start a new scope while for does not.

Note however that this rarely makes a practical difference and few people are even aware of it.

When people use for in ruby it's most often because that's what they're used to coming from other languages - not because of any differences between for and each.

like image 130
sepp2k Avatar answered Oct 16 '22 13:10

sepp2k