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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:44:49+00:00 2026-05-25T17:44:49+00:00

(1..5).each do|x| puts yield(x) end do |x| return x*2 end In my head this

  • 0
(1..5).each do|x| puts yield(x) end do |x| return x*2 end

In my head this would loop 1 through 5 call the first block that would yield to the second block and put 2,4,6,8,10

Why does this not work and whats the easiest way to write this.

  • 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-25T17:44:50+00:00Added an answer on May 25, 2026 at 5:44 pm

    yield works within the methods. Quote from “Programming Ruby”:

    Within the method, the block may be invoked, almost as if it were a
    method itself, using the yield statement.

    So, if you want to make this code working, you can change it to something like this:

    def f(n)
      (1..n).each do |x|
        puts yield(x)
      end
    end
    
    f(5) do |x|
      x * 2
    end
    

    If you don’t want to define method you should put block into the variable and then use it:

    b = Proc.new{|x| x *2 }
    
    (1..5).each do |x|
      puts b.call(x)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have this each loop in Ruby. @list.each { |i| puts i
I'm looking to run a script on a page that puts each piece of
This code randomizes 52 cards and puts 13 each in 4 box of text
I'm trying to loop through the elements in each but am having issues with
Each time a python file is imported that contains a large quantity of static
Each of these variables has an integer value. But this syntax is not valid
I discovered tonight that Ruby's Symbol#to_proc handles additional yielded values beyond the first as
def load_lib path = File.join(File.dirname(__FILE__), 'lib') failures = [] Dir.glob(#{path}/**/*.rb).each { |file| puts loading:
In this example from a blog post , class Array def each i =
My LaTeX puts each picture in my document to an empty page, no matter

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.