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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:05:38+00:00 2026-06-17T09:05:38+00:00

I am new to Ruby. I have used a lot of C# and JavaScript

  • 0

I am new to Ruby. I have used a lot of C# and JavaScript which allow higher-order functions and I typically use them on a daily basis.

Ruby seems a little strange to me though. An each function might look like:

def each
    @items.each do |item|
        yield(item)
    end
end

items.each { |item| puts item }

Yet Ruby also has some support for higher-order functions. The above could be rewritten to something like:

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

items.each -> (item) { puts item }        # Or...
items.each lambda { |item| puts item }

Or even:

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

# No difference in syntax.
items.each { |item| puts item }

Which is more on par with most other languages, and is just a few characters longer. Instead of explicitly passing in a block, everything seems to use yield.

yield itself seems crazy, magical, and mysterious. After all, it’s going to the origin of the call and grabbing a block immediately following the call. This seems bizarre and unnatural, and I’m not aware of any parallel of this feature in another language.

So what’s the deal with yield?

  • 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-17T09:05:39+00:00Added an answer on June 17, 2026 at 9:05 am

    Yield Passes Objects to a Method’s Block

    [Yield is] going to the origin of the call and grabbing a block immediately following the call.

    Not really. yield passes an argument to a block; it doesn’t “grab a block” or do anything with it. In other words, this:

    def foo; yield self; end
    foo { |x| x.inspect }                                       
    # => "main"
    

    Here, yield isn’t doing anything but passing an argument to the block that is passed into the foo method. Every Ruby method supports an optional block—except when a block is actually mandatory—so the only “magic” is that the language allows a block to be passed even when it isn’t explicitly declared as part of the method signature.

    Further Examples

    To see this implicit signature in action, consider this:

    def foo; puts block_given?; end
    foo { |x| x.inspect }
    

    which will print “true” and return nil, which is the expected return value from the puts method.

    Of course, without yield the block doesn’t do anything at all. For example:

    def foo; end
    foo { |x| x.inspect }
    # => nil
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to both ruby and rails, but have experience with java, javascript and
I am new to ruby and Aptana (I have used eclipse before) I was
I'm new with ruby and rails and have been struggling with this issue for
I am new to Ruby and Rails so bear with me please. I have
I'm new to Ruby on Rails, I know things but now I have to
I’m relatively new to Ruby, Sinatra, and DataMapper, but have a question about DataMapper
Complete new person to Ruby and Rails here... Have tried some tutorials in the
I am new to rails and I have gone through Kevin Skoglund's Ruby on
I'm new on both this site and ruby on rails! I have a common
I have built a very simple blog application using Ruby on Rails. New to

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.