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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:31:41+00:00 2026-06-10T13:31:41+00:00

Hi i just started learning ruby and i’m trying to loop through an array

  • 0

Hi i just started learning ruby and i’m trying to loop through an array i created looking like this:

server[0] = ['hostname' => 'unknown01', 'ip' => '192.168.0.2', 'port' => '22']
server[1] = ['hostname' => 'unknown02', 'ip' => '192.168.0.3', 'port' => '23']
server[2] = ['hostname' => 'unknown03', 'ip' => '192.168.0.4', 'port' => '24']

i tried using this code:

i=0
server[i].each do |x|
    print x['hostname']
    print x['ip']
    i+=1
end

but it only loops through server[0] how i can loop through server[0-3]

  • 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-10T13:31:42+00:00Added an answer on June 10, 2026 at 1:31 pm

    This probably isn’t doing what you think it is. Since you’re putting your key/value pairs inside of square brackets, it winds up being an array of one hash (other languages call them maps and dictionaries). Probably you want to exchange your square brackets with curly brackets like this:

    servers = []
    servers[0] = {'hostname' => 'unknown01', 'ip' => '192.168.0.2', 'port' => '22'}
    servers[1] = {'hostname' => 'unknown02', 'ip' => '192.168.0.3', 'port' => '23'}
    servers[2] = {'hostname' => 'unknown03', 'ip' => '192.168.0.4', 'port' => '24'}
    

    But since you’re just setting each one by its index, you don’t need to build it up this way, you can just place them in their respective positions within the array.

    servers = [
      {'hostname' => 'unknown01', 'ip' => '192.168.0.2', 'port' => '22'},
      {'hostname' => 'unknown02', 'ip' => '192.168.0.3', 'port' => '23'},
      {'hostname' => 'unknown03', 'ip' => '192.168.0.4', 'port' => '24'},
    ]
    

    Then to iterate over each one you can do:

    servers.each do |server|
      puts server['hostname']
      puts server['ip']
      puts
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started learning ruby, so this question is simple. I created @subject in
I just started learning some ruby, and I want to do something like this:
I've only just started learning ruby on rails and I would like to create
Just started learning Ruby metaprogramming. Looking at Object.methods I get: Object.methods => [ :allocate,
I just started learning C but I don't understand this part of the code.
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
I just started learning Common Lisp a few days ago, and I'm trying to
I just started learning Ruby. I typed the example : x = 10 5.times
I have just started learning ruby on rails. I have a doubt wrt routing.
I have just started learning Ruby on Rails. I happened to look for prevention

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.