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

Related Questions

Just had a quick question to throw out and see if there was a
I just had a quick question for all devs out there, what does +=
I just had a quick question about git - how backwards compatible are git
I am new to D3 and just had a quick question about tick labels
I just had a question about aligning two expand-collapse boxes and got an answer
I had a quick question regarding the datacontractserializer. Maybe it's more of a stream
I am new to Ruby and had a quick question. I am trying to
Alrighty, coding in Perl and just had a quick question. I have class created
New to OOP with Perl, and just had a quick question. I have this
I'm new to iOS development, just had a quick question. I'm creating an app

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.