Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:55:32+00:00 2026-05-16T00:55:32+00:00

I’m learning Ruby and RoR at the moment, and I came across this: <%

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:55:33+00:00Added an answer on May 16, 2026 at 12:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.