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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:17:56+00:00 2026-05-24T09:17:56+00:00

I have a Ruby hash that looks like this: h = {side1=>[green, Green], side2=>[verde,

  • 0

I have a Ruby hash that looks like this:

h = {"side1"=>["green", "Green"], "side2"=>["verde", "Verde"]}

How can I get the first (or last) item at a particular key in the hash?

  • 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-24T09:17:56+00:00Added an answer on May 24, 2026 at 9:17 am

    Actually, your example pseudo code in your question is correct.

    For your hash

    h = {
       "side1" => ["green", "Green"], 
       "side2" => ["verde", "Verde"]
    }
    

    the keys are 'side1' and 'side2' (with their values right of the hashrocket)

    So, h['side2'].first says that for the value of key 'side2', get the first element in the value ["verde", "Verde"], which is an array. h['side2'][0] would also work. the alias first is a convenience method.

    To get the last element of an array without having to know how big it is, use a negative index. e.g. h['side2'][-1], which is equivalent to h['side2'][1] in this case.

    Note that keys in a hash are particular about whether it is a string or symbol. That is h[:side2] would return nil, as the key hasn’t been set. Rails has a class HashWithIndifferentAccess that treats symbols and strings as equivalent keys.

    EDIT

    I should have mentioned that I tested my answer in irb. irb is a great way to test your ideas about what may and may not work. In your terminal prompt, type irb, enter, and play with ruby.

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

Sidebar

Related Questions

I have a ruby hash that looks like this { stuff_attributes => { 1
Let's say I have a hash in Ruby like this: d = {1 =>
I have a node that looks like this, in the xml file: <property name=nameA
In Ruby 1.9.x I have a hash that maintains its order hsh = {9=>2,
I have a Ruby hash that contains a user-generated string. I need to pass
How can I, very simply construct a hash in ruby using something simple like
I have a hash object, whose structure looks similar to this: {:category1 => {:subcategory1
I have a hash in Ruby that is storing the word frequency of a
I'm kinda new to Flex. I have trying to send Hash from Ruby on
so i have ruby script that simply writes to a test.txt file with hello

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.