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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:36:19+00:00 2026-06-03T04:36:19+00:00

In many cases over internet i seen examples of each method for Enumerable as:

  • 0

In many cases over internet i seen examples of each method for Enumerable as:

def each(&block)
  @items.each do |item|
    block.call(item)
  end
end

Why people not using this one:

def each(&block)
  @items.each(&block)
end

Is there any differences?

  • 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-06-03T04:36:20+00:00Added an answer on June 3, 2026 at 4:36 am

    Actually I think the following version is even more commonly seen:

    def each
      @items.each { |i| yield i }
    end
    

    This is equivalent to your first code sample. There is however a subtle difference between this and your second version:

    class Test
      def initialize(items)
        @items = items
      end
    
      def each1
        @items.each { |i| yield i }
      end
    
      def each2(&block)
        @items.each(&block)
      end
    end
    

    Observe:

    irb(main):053:0> Test.new([1,2,3]).each2
    => #<Enumerator: [1, 2, 3]:each>
    irb(main):054:0> Test.new([1,2,3]).each1
    LocalJumpError: no block given (yield)
        from (irb):43:in `block in each1'
        from (irb):43:in `each'
        from (irb):43:in `each1'
        from (irb):54
        from /usr/bin/irb:12:in `<main>'
    

    The version that delegates the block to the underlying iterable actually returns an enumerator if no block is given, which is very nice. It allows us to write stuff like this:

    irb(main):055:0> Test.new([1,2,3]).each2.map { |x| x + 1 }
    => [2, 3, 4]
    

    To achieve the same with our explicit version, we’d have to adapt it like this:

    def each1
      return enum_for(:each1) unless block_given?
      @items.each { |i| yield i }
    end
    

    Which is even more verbose. Bottomline: Delegate the block to an underlying enumerable whenever possible, to save yourself from code duplication and from subtle gotchas like these.

    By the way, now that you realize the two-folded nature of your each method, it would make sense to name it differently. For example, you could follow the example of methods like String#chars or IO#lines and call your method items:

    def items(&block)
      @items.each(&block)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In many prototype scripting cases, it's easier to just stick every form item (such
I know there are many ways over the internet but this case is a
I know there are many cases which are good cases to use multi-thread in
First of all, there are many cases where Sleep() is misused , for example
I'm using c#. In many cases I'm writing code that can benefit from a
In Python scripts, there are many cases where a keyboard interrupt (Ctrl-C) fails to
I'd like to use properties for my instance variables, but in many cases, I
In many cases, there are two implementation choices: a closure and a callable class.
In our applications, I can't think of many cases where we care about null
The .NET 4.5 framework libraries integrate C#-style Task-based async fairly extensively. In many cases,

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.