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

  • Home
  • SEARCH
  • 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 6932667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:44:05+00:00 2026-05-27T11:44:05+00:00

I have been learning ruby and interested in knowing how ‘each’ is implemented in

  • 0

I have been learning ruby and interested in knowing how ‘each’ is implemented in the array class. I saw one documentation here and it looks like this is how ‘each’ is written;

# within class Array...
def each
  for each element
    yield(element)
  end
end

I did exactly write the code above (without the comment#) in the ruby console (I’m using 1.9.2) and got this syntax error

:SyntaxError: (irb):2: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
(irb):5: syntax error, unexpected keyword_end, expecting $end

Is this happening due to incomplete array class implementation (i.e ‘element’ is not defined or is this because of anything else? I’d also like to know how ‘each’ and other basic fnctions are implemented. Any reference to the right documentation/answers would be helpful. Let me know if this is a duplicate (i didnt see any similar questions). thanks

  • 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-27T11:44:05+00:00Added an answer on May 27, 2026 at 11:44 am

    First off, the syntax of your for statement is off, it should be something like for element in elements, which is almost equivalent to elements.each { |element| ... } except that it doesn’t introduce a new scope. In fact for is implemented using each, as can be seen when you try to call it on a method that has no defined each method:

    >> for element in nil
    ..   end
    NoMethodError: undefined method `each' for nil:NilClass
        from (irb):1
    

    Regarding your syntax error: since you are reopening a class, when the Ruby parser sees the standalone each it uses self as the receiver, so it translates your statement to for self.each element, where element is the tIDENTIFIER mentioned, whereas something like self.each do |element| ... end would have been expected.

    As for the implementation of Array#each, it’s implemented in C and looks like this

    VALUE
    rb_ary_each(VALUE array)
    {
        long i;
        volatile VALUE ary = array;
    
        RETURN_ENUMERATOR(ary, 0, 0);
        for (i=0; i<RARRAY_LEN(ary); i++) {
            rb_yield(RARRAY_PTR(ary)[i]);
        }
        return ary;
    }
    

    This is basically what you tried to write in Ruby in C.

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

Sidebar

Related Questions

I've been learning Ruby for a class and have been writing a sample game.
I have been learning Ruby lately, and I want to upload a test web
I am currently in the process of learning Ruby on Rails. I have been
I have been learning Ruby & more specifically the rails framework the past few
I have been learning to use a slideshow code I saw on a website
I have been learning Ruby (Rails/Sinatra) and it's good very good but OMFG how
I have started learning ruby and i have been looking at ruby on rails.Is
I have started learning Ruby on Rails and I have been stuck at something
I still learning Ruby on Rails, but have been wondering what happens if I
I have been learning about the basics of C# but haven't come across a

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.