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
Considering that everything in Ruby is an object and we can open irb and
In irb data = 2 1058 657682 barcode = Barby::QrCode.new(data) File.open('barcode.png', 'w+') {|f| f.write
Open this page : http://jsfiddle.net/dwDZx/6/ Resize until red Continue make the browser smaller <div
I'm trying to read Stanford ecorner XML: open(http://ecorner.stanford.edu/RecentlyAdded.xml) but am running into the following
When I run this code in irb: File.open('j1.txt','w') {|f| f.write(doc1223423)} it works successfully. But
I am on Ubuntu10 sudo apt-get install ruby1.9.1-full then download sources of rubygem 1.3.7
I'm building an app in Rails that needs to convert a Twitter ID into
open( my $handle, '<', 'file.dat' ) or die $!; my @data = map {
open(my $fh, '>', $path) || die $!; my_sub($fh); Can my_sub() somehow extrapolate $path from

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.