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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:23:04+00:00 2026-05-18T08:23:04+00:00

I encountered the following Ruby code: class MyClass attr_accessor :items … def each @items.each{|item|

  • 0

I encountered the following Ruby code:

class MyClass
    attr_accessor :items
    ...
    def each
        @items.each{|item| yield item}
    end
    ...
end

What does the each method do? In particular, I don’t understand what yield does.

  • 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-18T08:23:04+00:00Added an answer on May 18, 2026 at 8:23 am

    This is an example fleshing out your sample code:

    class MyClass
      attr_accessor :items
    
      def initialize(ary=[])
        @items = ary
      end
    
      def each
        @items.each do |item| 
          yield item
        end
      end
    end
    
    my_class = MyClass.new(%w[a b c d])
    my_class.each do |y|
      puts y
    end
    # >> a
    # >> b
    # >> c
    # >> d
    

    each loops over a collection. In this case it’s looping over each item in the @items array, initialized/created when I did the new(%w[a b c d]) statement.

    yield item in the MyClass.each method passes item to the block attached to my_class.each. The item being yielded is assigned to the local y.

    Does that help?

    Now, here’s a bit more about how each works. Using the same class definition, here’s some code:

    my_class = MyClass.new(%w[a b c d])
    
    # This points to the `each` Enumerator/method of the @items array in your instance via
    #  the accessor you defined, not the method "each" you've defined.
    my_class_iterator = my_class.items.each # => #<Enumerator: ["a", "b", "c", "d"]:each>
    
    # get the next item on the array
    my_class_iterator.next # => "a"
    
    # get the next item on the array
    my_class_iterator.next # => "b"
    
    # get the next item on the array
    my_class_iterator.next # => "c"
    
    # get the next item on the array
    my_class_iterator.next # => "d"
    
    # get the next item on the array
    my_class_iterator.next # => 
    # ~> -:21:in `next': iteration reached an end (StopIteration)
    # ~>    from -:21:in `<main>'
    

    Notice that on the last next the iterator fell off the end of the array. This is the potential pitfall for NOT using a block because if you don’t know how many elements are in the array you can ask for too many items and get an exception.

    Using each with a block will iterate over the @items receiver and stop when it reaches the last item, avoiding the error, and keeping things nice and clean.

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

Sidebar

Related Questions

I am debugging some code and have encountered the following SQL query (simplified version):
I'm an intermediate C++ user and I encountered the following situation. The class definition
While converting an old code, I encountered the following problem. Given an HTML string,
I encountered the following code in a file (looks like header file) iomanip of
I've been doing an Android tutorial and encountered a class with the following: public
I encountered the following code in a project that I'm working with. i don't
I encountered the following PL/SQL code(variables modified) at work: PROCEDURE test(i_w IN a.w%type,o_result IN
I encountered the following ddl in a pl/sql script this morning: create index genuser.idx$$_0bdd0011
Has anyone encountered the following problem: I have IIS7 running on my computer. On
We've encountered the following issue. I like to use the following writing: SELECT Id,

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.