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 944059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:31:54+00:00 2026-05-15T22:31:54+00:00

I just had a quick question regarding loops in Ruby. Is there a difference

  • 0

I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection?

# way 1
@collection.each do |item|
  # do whatever
end

# way 2
for item in @collection
  # do whatever
end

Just wondering if these are exactly the same or if maybe there’s a subtle difference (possibly when @collection is nil).

  • 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-15T22:31:55+00:00Added an answer on May 15, 2026 at 10:31 pm

    This is the only difference:

    each:

    irb> [1,2,3].each { |x| }
      => [1, 2, 3]
    irb> x
    NameError: undefined local variable or method `x' for main:Object
        from (irb):2
        from :0
    

    for:

    irb> for x in [1,2,3]; end
      => [1, 2, 3]
    irb> x
      => 3
    

    With the for loop, the iterator variable still lives after the block is done. With the each loop, it doesn’t, unless it was already defined as a local variable before the loop started.

    Other than that, for is just syntax sugar for the each method.

    When @collection is nil both loops throw an exception:

    Exception: undefined local variable or method `@collection’ for main:Object

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

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's nothing special you need to do - just reference… May 16, 2026 at 5:02 pm
  • Editorial Team
    Editorial Team added an answer This look indeed like a bug in the EL implementation… May 16, 2026 at 5:02 pm
  • Editorial Team
    Editorial Team added an answer The server where it doesn't work is sending the CSS… May 16, 2026 at 5:02 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I had a quick question regarding the datacontractserializer. Maybe it's more of a stream
We've just had a discussion with college about is the following style acceptable for
I'm a beginner at rails. And I've come to understand two different ways to
I'm new to MDX and have a problem regarding filtering out values from a
(I am sorry, I wanted to ask this question on meta first, but it
I've had a look round similar articles such as this one and I can't
Had finally gotten all the bugs out of this and now they said Oh,
Does anyone have any good starting points for me when looking at making web
Ok, so i'm basically trying to load the contents of a .txt file that
I'm doing some reading on SQL Server performance: http://www.amazon.com/Inside-Microsoft-SQL-Server-2005/dp/0735623139/ref=sr_1_6?ie=UTF8&s=books&qid=1267032068&sr=8-6 One of the surprising things

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.