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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:29:19+00:00 2026-05-23T20:29:19+00:00

I recently started learning ruby, and I understood that you coud use code blocks

  • 0

I recently started learning ruby, and I understood that you coud use code blocks with both of these syntaxes. But I just found a case which I dont understand:

#my_hash is a hash in which the keys are strings and the values arrays, but dont think about the specifics fo the code

#if I run my code like this, it works perfectly

my_hash.each do |art|
  puts mystring.gsub(art[0]).each {
    art[1][rand(art[1].length) -1]
  }
end

#but if I use this, it prints "Enumerator"

my_hash.each do |art|
  puts mystring.gsub(art[0]).each do
    art[1][rand(art[1].length) -1]
  end
end

Is it because you cant nest do-end pairs?
I am using 1.9

  • 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-23T20:29:19+00:00Added an answer on May 23, 2026 at 8:29 pm
    puts mystring.gsub(art[0]).each do
      art[1][rand(art[1].length) -1]
    end
    

    Here you called puts without parens, the do ... end refers to the puts method, that does nothing with a block and prints mystring.gsub(art[0]).each (with is a Enumerator).

    The { ... } is called with the nearest method. Becomes ugly, but you can do it with do ... end:

    puts(mystring.gsub(art[0]).each do
      art[1][rand(art[1].length) -1]
    end)
    

    Or, better, put the result in a variable and print the variable:

    var = mystring.gsub(art[0]).each do
      art[1][rand(art[1].length) -1]
    end
    puts var
    

    Anyway, the each don’t changes the object, it just iterate and returns the object itself. You may be wanting the map method, test it.

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

Sidebar

Related Questions

I've just recently started learning/using Ruby at work. I've got a super-simple test script
I have recently started learning Ruby, as my first programming language. I feel comfortable
I have started learning Ruby recently and I was trying out the following piece
I have recently started learning C++ and coming from a Ruby environment I have
I've recently started learning Ruby on Rails, based on RoR3 beta/RC. I had earlier
I've recently started learning XS using perlxstut and the tutorial suggests that I create
I recently started learning Ruby and Ruby on Rails, and have watched a plethora
I just recently started learning Java. I have a question which is more about
I've recently started learning how to use MFC in Visual Studio. My program uses
I recently just started learning .NET, and more specifically I've been playing around with

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.