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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:15:38+00:00 2026-05-20T10:15:38+00:00

I’m trying to create a simple hash to look up for corresponding strings to

  • 0

I’m trying to create a simple hash to look up for corresponding strings to some symbols (:student_number, :first_name, …). I’m having issue retrieving data from this function though. Here’s the function

Snippet A

  def get_nice_column_name(col_symbol)
    column_names = { 
      :first_name => "Student's First Name",
      :last_name => "Student's Last Name", 
      :email => "Student's Email", 
      :given_name => "Student's Given Name"
    }
    return column_names[col_symbol]
  end

Here’s how I use it, but not working:

Snippet B

col_titles = []
params = {:first_name => 'true', 
          :last_name => 'true', 
          :email => 'true', 
          :given_name => 'true' }

params.each do |key, value|
  if ( value == 'true')
    col_titles << get_nice_column_name(key)
  end
end

When I look into col_titles, I expect [“Student’s First Name”, “Student’s Last Name”], but I actually don’t get anything, just [] empty array.

I thought that was weird so I tried printing out the object_id of the symbols (col_symbol in snippet A) and the symbols in the hash column_names, I get different object_ids. I’m wondering why they are different (they both present the same symbols). If I add this to the function get_nice_column_name in Snippet A:

    puts "col_symbol is " + col_symbol.object_id.to_s + ", while :first_name is " + (:first_name).object_id.to_s
    puts "col_symbol is " + col_symbol.object_id.to_s + ", while :last_name is " + (:last_name).object_id.to_s
    puts "col_symbol is " + col_symbol.object_id.to_s + ", while :email is " + (:email).object_id.to_s
    puts "col_symbol is " + col_symbol.object_id.to_s + ", while :given_name is " + (:given_name).object_id.to_s

I would get this in the console

col_symbol is 98351040, while :first_name is 1221688
col_symbol is 98351040, while :last_name is 580888
col_symbol is 98351040, while :email is 168888
col_symbol is 98351040, while :given_name is 1290648

983541040 doesn’t really match any of the {1221688, 580888, 168888, 1290648}. Is this why my get_nice_column_name is useless ? Because the symbols are different under the hood?

Thanks for your help guys !

Regards

  • 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-20T10:15:38+00:00Added an answer on May 20, 2026 at 10:15 am

    If you are using Ruby 1.9 this is a bit more concise way to do it.

    column_names = { 
      :first_name => "Student's First Name",
      :last_name  => "Student's Last Name",
      :email      => "Student's Email",
      :given_name => "Student's Given Name"
    }
    
    params = {
      :first_name => 'FirstName',
      :last_name  => 'LastName',
      :email      => 'Email',
      :given_name => 'GivenName'
    }
    col_titles = []
    
    col_titles = column_names.values
    data = params.values_at(*column_names.keys)
    
    col_titles # => ["Student's First Name", "Student's Last Name", "Student's Email", "Student's Given Name"]
    data # => ["FirstName", "LastName", "Email", "GivenName"]
    

    This takes advantage of Ruby 1.9’s new Hash behavior, where Ruby remembers the order of insertion and will honor that order when retrieving the keys and values. You can do something similar with 1.8, but you’d have to define the order of the columns in an array, then use that to pull the column headings and values of the data instead of relying on column_names to set the order.

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

Sidebar

Related Questions

No related questions found

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.