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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:39:46+00:00 2026-06-04T17:39:46+00:00

This is my first time working with Ruby, so I may be approaching this

  • 0

This is my first time working with Ruby, so I may be approaching this incorrectly.

I am trying to go through a hash to display it’s contents. As I’m going through the hash I’ll need to test if a key exists, like city. If city doesn’t exist then it shouldn’t display the address. This is where I’ve started with building my hash:

# app.rb
set :haml, :format => :html5
get "/" do
    @users = Hash[
                  [["name", "bill"], ["city", "nyc"],  ["address", "street"]],
                  [["name", "ted"], ["city", "denver"]],
                  [["name", "sam"], ["address", "road"]]
                 ]
    haml :index
end

And this is how I am looping through the hash:

# layout.haml
- @users.each do |user|
    - user.each do |u|
        - u.each do |b|
            = b

Once I get to b it will display all of the content like so:

["name", "bill"]
["city", "nyc"]
["address", "street"]
["name", "ted"]
["city", "denver"]

In the loop, how can I display the name as well as check to see if the address exists for each user to determine if the city should be displayed as well as any markup that may need to be added? It would ideally display something like:

<p>bill, <span class="address">nyc, street</span></p>
<p>ted</p>
<p>sam, <span class="address">road</span></p>

Am I creating the Hash properly to do it this way?

  • 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-04T17:39:47+00:00Added an answer on June 4, 2026 at 5:39 pm

    Instead of what you are trying to do with nested arrays inside a hash, it would be better to have an array that contains user hashes:

    @users = [
               { :name => 'bill', :city => 'city', :address => 'street' },
               { :name => 'ted', :city => 'denver' },
               { :name => 'sam', :address => 'road' }
             ]
    

    With that, you can do something like this:

    - @users.each do |user|
        = user[:name]
        - if user.has_key?(:address) && user.has_key?(:city)
            = "#{user[:address]}, #{user[:city]}"
        - elsif user.has_key?(:address)
            = "#{user[:address]}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first time making a hash table. I'm trying to associate strings
This is my first time working with localStorage, and I'm trying to store a
This is my first time working with regular expressions and I've been trying to
This is my first time working with Quartz. I am trying out a sample
This is my first time working with file i/o in java, and it's not
This is the first time I'm trying random numbers with C (I miss C#).
this is my first time trying to use document classes in AS3 and im
I am currently working on an ASP.NET MVC2 project. This is the first time
This is my first time working with a webservice and I tried looking up
This question arises out of a combination of this being my first time working

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.