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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:17:41+00:00 2026-05-23T03:17:41+00:00

When i open IRB and paste h = {colors => [red, blue, green], letters

  • 0

When i open IRB and paste

h = {"colors"  => ["red", "blue", "green"],
        "letters" => ["a", "b", "c" ]}
h.assoc("letters")  #=> ["letters", ["a", "b", "c"]]
h.assoc("foo")      #=> nil

into it i always get the message:

NoMethodError: undefined method `assoc' for {"letters"=>["a", "b", "c"], "colors"=>["red", "blue", "green"]}:Hash
from (irb):3
from :0

although this code is taken from http://ruby-doc.org/core/classes/Hash.html#M000760
What am i doing wrong?

  • 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-23T03:17:42+00:00Added an answer on May 23, 2026 at 3:17 am

    Hash#assoc is a Ruby 1.9 method, and is not available in Ruby 1.8 (which you’re probably using).

    If you wanted the same results, you could just do

    ["letters", h["letters"]]
    # => ["letters", ["a", "b", "c"]]
    

    You could patch in similar behavior in Ruby 1.8 too:

    class Hash
      def assoc(key_to_find)
        if key?(key_to_find)
          [key_to_find, self[key_to_find]]
        else
          nil
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't get rb-appscript to activate a specific Safari window. It is always activating
In irb data = 2 1058 657682 barcode = Barby::QrCode.new(data) File.open('barcode.png', 'w+') {|f| f.write
I'm trying to read Stanford ecorner XML: open(http://ecorner.stanford.edu/RecentlyAdded.xml) but am running into the following
I open a TcpClient and then call tcpClient.GetStream().Read(message, 0, 8) in order to read
When I run this code in irb: File.open('j1.txt','w') {|f| f.write(doc1223423)} it works successfully. But
Open source implementation will be preferred.
Open up a Rails console and enter this: 2.weeks.ago.between? 2.weeks.ago, 1.week.ago Did it give
Open Ankh seems to be using Subversion 1.6.2 and Tortoise uses Subversion 1.6.0. After
The Open button on the open file dialog used in certain windows applications includes
The Open/Closed Principle states that software entities (classes, modules, etc.) should be open for

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.