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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:08:05+00:00 2026-06-17T21:08:05+00:00

I have an array of hashes in the format albums = [ {name: Sgt.

  • 0

I have an array of hashes in the format

albums = [ {name: "Sgt. Pepper's Lonely Hearts Club Band", year: "1967" }, 
           { name: "Are You Experienced?", year: "1967" } 
         ]

etc..

I am trying to insert this output into a string that then needs to be inserted into html. I currently have this

def convert_to_html albums
  string_before = 
    "<html>
      <head>
        <title>\"Rolling Stone's ....\"</title>
      </head>
      <body>
        <table>\n" + 
        albums.each do |x| 
          "name is: #{x.values[0]} year is: #{x.values[1]}"
        end  + "
        </table>
      </body>
    </html>"
end

I know that this probably isn’t the best way to get the values but I can’t figure out any other way, but my major problem is that when I try this I get this error:

[2013-01-27 00:16:20] ERROR TypeError: can't convert Array into String
    albums.rb:72:in `+'
    albums.rb:72:in `convert_to_html'
    albums.rb:28:in `block in render_list'
    albums.rb:26:in `open'
    albums.rb:26:in `render_list'
    albums.rb:9:in `call'

Is there any way to insert the values from each hash side by side into the string?

P.S. I used name and year for readability. I know #{x.values[0]} #{x.values[1]} is the correct way to format 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-06-17T21:08:06+00:00Added an answer on June 17, 2026 at 9:08 pm

    This is simple code showing how to do it:

    album_list = [
      {name: "Sgt. Pepper's Lonely Hearts Club Band", year: "1967" }, 
      { name: "Are You Experienced?", year: "1967" } 
    ]
    
    def convert_to_html(albums)
    
    "<html>
      <head>
        <title>\"Rolling Stone's ....\"</title>
      </head>
      <body>
        <table>
    " + albums.map { |album| 
    "     <tr><td>name is: #{ album[:name] } year is: #{ album[:year] }</td></tr>"
    }.join("\n") +
    "
        </table>
      </body>
    </html>"
    
    end
    
    puts convert_to_html(album_list)
    

    Which outputs:

    <html>
      <head>
        <title>"Rolling Stone's ...."</title>
      </head>
      <body>
        <table>
        <tr><td>name is: Sgt. Pepper's Lonely Hearts Club Band year is: 1967</td></tr>
        <tr><td>name is: Are You Experienced? year is: 1967</td></tr>
        </table>
      </body>
    </html>
    

    In real life I’d use ERB or HAML to generate the HTML. They’re great templating tools.

    Other ways of writing:

    <tr><td>name is: #{ album[:name] } year is: #{ album[:year] }</td></tr>"
    

    Are:

    <tr><td>name is: %s year is: %s </td></tr>" % [ album[:name], album[:year] ] 
    

    or:

    <tr><td>name is: %s year is: %s </td></tr>" % album.values_at(:name, :year)
    

    I don’t recommend using album.values by itself because it relies on the order of insertion into the album hash. In the future, if you modify the hash by putting something into it in a different order while maintaining the code, the values order will change, breaking it in a way that might not be obvious.

    Instead, use one of these ways of accessing the values to always explicitely get the value associated with the key. That’s part of programming defensively.

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

Sidebar

Related Questions

I have an array of hashes which look like: ward = {id: id, name:
I have an array of hashes. rabbits = [{:color=>blue, :height=>5, :name=>Charles}, {:color=>red, :height=>12, :name=>Henry},
I have an array of hashes. Each entry looks like this: - !map:Hashie::Mash name:
I have this array of hashes: results = [ {day=>2012-08-15, name=>John, calls=>5}, {day=>2012-08-15, name=>Bill,
I have this array of hashes: - :name: Ben :age: 18 - :name: David
I have a array of hashes like: detail = [{'name': 'Adam'}, {'name': 'Jackie'}] Now
If I have an array of hashes, each with a day key: [ {:day=>4,:name=>'Jay'},
I have array of hashes: @array = [{:id => 1, :status=>R}, {:id => 1,
I have an array of hashes. Each hash looks like: 'date'=>6/23/2011, value1=>6, value2=>8, value3=>3,
In Ruby, I have an array of hashes and an array. In my array

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.