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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:35:29+00:00 2026-05-18T09:35:29+00:00

This is probably easy to do! I’m not able envision the loop yet, I

  • 0

This is probably easy to do! I’m not able envision the loop yet, I was thinking about a nested for loop but not quite sure how to alternate between the two hashes.

Lets say I have a class with a def that containts two hash tables:

 class Teststuff
    def test_stuff
     letters = { "0" => " A ", "1" => " B ", "2" => " C " }
     position = {"1" => "one ", "2"=> " two ", "3"=> " three ", "4"=>" four " }

     my_array=[0,1,2,2] #this represents user input stored in an array valid to 4 elements
     array_size = my_array.size #this represents the size of the user inputed array
     element_indexer = my_array.size # parellel assignment so I can use same array for array in dex
     array_start_index = element_indexer-1 #give me the ability later to get start at index zero for my array

 #for loop?? downto upto?? 
 # trying to get loop to grab the first number "0" in element position "0", grab the hash values then
 # the loop repeats and grabs the second number "1" in element position "1" grab the hash values
 # the loop repeats and grabs the third number "2" in elements position "2" grab the hash values
 # the final iteration grabs the fourth number "2" in elements position "3" grab the hash values
 # all this gets returned when called. Out put from puts statement after grabing hash values 
 # is: **A one B two C three C four**  

     return a_string
    end
  end  

How do I go about returning string output to the screen like this:

   **A one B two C three C four** 

or simply letter position letter position…
Thanks for the help, put code up so I can try on my editor!

  • 1 1 Answer
  • 1 View
  • 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-18T09:35:30+00:00Added an answer on May 18, 2026 at 9:35 am

    I think I figured out what it is you want, although I still have no idea what array_size, element_indexer, array_start_index and TestStuff are for.

    def test_stuff
      letters = { "0" => " A ", "1" => " B ", "2" => " C " }
      position = {"1" => "one ", "2"=> " two ", "3"=> " three ", "4"=>" four " }
    
      my_array = [0, 1, 2, 2]
    
      "**#{my_array.map.with_index {|e, i|
        "#{letters[e.to_s].strip} #{position[(i+1).to_s].strip}"
      }.join(' ')}**"
    end
    

    [I took the liberty of reformatting your code to standard Ruby coding style.]

    However, everything would be much simpler, if there weren’t all those type conversions, and all those superfluous spaces. Also, the method would be much more useful, if it actually had a way to return different results, instead of always returning the same thing, because at the moment, it is actually exactly equivalent to

    def test_stuff
      '**A one B two C three C four**'
    end
    

    Something along these lines would make much more sense:

    def test_stuff(*args)
      letters = %w[A B C]
      position = %w[one two three four]
    
      "**#{args.map.with_index {|e, i| "#{letters[e]} #{position[i]}" }.join(' ')}**"
    end
    
    test_stuff(0, 1, 2, 2)
    # => '**A one B two C three C four**'
    

    If you don’t want to pollute the Object namespace with your method, you could do something like this:

    def (TestStuff = Object.new).test_stuff(*args)
      letters = %w[A B C]
      position = %w[one two three four]
    
      "**#{args.map.with_index {|e, i| "#{letters[e]} #{position[i]}" }.join(' ')}**"
    end
    
    TestStuff.test_stuff(0, 1, 2, 2)
    # => '**A one B two C three C four**'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably really easy, but I'm lost on how to make sure it
This is probably an easy solution but Im not getting it. So I have
This is probably an easy question, but I haven't been able to find a
This probably has an easy answer, but I haven't been able to find one
This is probably easy but I am getting stuck: when I build a solution
This is probably an easy question, but I want to understand better how Apache
This is probably really easy but I can't seem to figure out how to
This is probably seriously easy to solve for most of you but I cannot
Morning y'all This is probably an easy one but I barely got any sleep
Ok, I think this is probably an easy question but for the life of

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.