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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:27:36+00:00 2026-06-15T08:27:36+00:00

I’m trying to access a hash by using an regular class method like this:

  • 0

I’m trying to access a hash by using an regular class method like this:

aHash.key

But I get this error:

undefined method `key1′ for {:key1=>”value1″, :key2=>”value2″}:Hash

I defined a ‘method_missing’ in a Hash class as suggested here:

class Hash
  def method_missing(method, *params)
    method = method.to_sym
    return self[method] if self.keys.collect(&:to_sym).include?(method)
    super
  end
end

I know for sure, that when I call ‘aHash.key’ it does not use the Hash I defined. If I look at one on my gems, it shows this:

require “activerecord-postgres-hstore/hash”

So I checked this file, and indeed they have implemented another hash. I believe I should add ‘method_missing’ in there but can it be done without modified the gem?

Maybe I’m not getting how class overloading works with ruby, so the answer might be something else.

  • 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-15T08:27:37+00:00Added an answer on June 15, 2026 at 8:27 am

    Like SporkInventor said, what you are doing is very dangerous and bad practice, you shouldn’t patch Hash. Instead, like he said, use OpenStruct from Ruby’s standard library. If you need, change your code to wrap the Hash into an OpenStruct when returning it.

    require 'ostruct'
    result = OpenStruct.new({ :a => 1, :b => 2 })
    result.a # => 1
    

    So if you really need to monkey-patch, rather monkey patch the method that is returning the hash to wrap the result in an OpenStruct, instead of patching Hash. Or if you need it to really be a Hash (obj.is_a? Hash #=> true) then create a subclass of hash with your method_missing implementation and then monkey-patch the method returning the Hash to return your OpenHash (or whatever you call it).

    class OpenHash < Hash
      def self.from_hash(hash)
        h = OpenHash.new
        h.merge!(hash)
        h
      end
    
      def method_missing(method, *params)
        method = method.to_sym
        return self[method] if self.keys.collect(&:to_sym).include?(method)
        super
      end
    end
    

    For (made up) example you have a Hstore class which has a method return_hash which returns the hash. Then patch it like this:

    class Hstore
      def return_hash_with_ostruct
        OpenStruct.new(return_hash_without_ostruct)
      end
      alias_method_chain :return_hash, :ostruct
    end
    Hstore.new.return_hash.class # => OpenStruct
    

    Monkey-patching standard library is very bad practice!

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.