45

I have a a hash

foo = {'bar'=>'baz'}

I would like to call foo.bar #=> 'baz'

My motivation is rewriting an activerecord query into a raw sql query (using Model#find_by_sql). This returns a hash with the SELECT clause values as keys. However, my existing code relies on object.method dot notation. I'd like to do minimal code rewrite. Thanks.

Edit: it appears Lua has this feature:

point = { x = 10, y = 20 }   -- Create new table
print(point["x"])            -- Prints 10
print(point.x)               -- Has exactly the same meaning as line above
2
  • 4
    Why does it matter that Lua has this "feature"? So does JavaScript. It's not particularly germane to the question.
    – Jim Puls
    Nov 18, 2009 at 3:57
  • 5
    I was just trying to better illustrate what I desired to accomplish with Ruby.
    – user94154
    Nov 21, 2009 at 1:44

5 Answers 5

81
>> require 'ostruct'
=> []
>> foo = {'bar'=>'baz'}
=> {"bar"=>"baz"}
>> foo_obj = OpenStruct.new foo
=> #<OpenStruct bar="baz">
>> foo_obj.bar
=> "baz"
>>
4
  • 4
    Use of OpenStruct is discouraged since each instantiation invalidates the Ruby method cache, which slows down your entire application. Jun 14, 2015 at 22:12
  • @ChrisHeald: care to elaborate a bit on this proposition? Who discourages the use of OpenStruct? Does it invalidate the entire method cache or just the method cache for the symbol?
    – Steen
    Jul 16, 2015 at 8:31
  • 4
    @Steen If you google "ruby method cache" the first couple of hits will be explanations of it by Charlie Somerville and the late James Golick. Before Ruby 2.1, instantiating an ostruct method dumped the entire method cache. In 2.1+, it dumps the method cache for all ostruct instances and instances of its subclasses. Jul 16, 2015 at 15:57
  • 1
    This is not recursive ! Prefer @gabor solution Feb 11, 2017 at 15:24
35

What you're looking for is called OpenStruct. It's part of the standard library.

22

A good solution:

class Hash
  def method_missing(method, *opts)
    m = method.to_s
    if self.has_key?(m)
      return self[m]
    elsif self.has_key?(m.to_sym)
      return self[m.to_sym]
    end
    super
  end
end

Note: this implementation has only one known bug:

x = { 'test' => 'aValue', :test => 'bar'}
x.test # => 'aValue'

If you prefer symbol lookups rather than string lookups, then swap the two 'if' condition

6
  • Might not have solved his problem, but this solved one of mine. Took a lot of searching to find this, but I thank you!
    – gregf
    Nov 3, 2010 at 17:30
  • 3
    I've been doing this self[m] || self[m.to_s] || super instead of the if/else block
    – Kyle
    Sep 19, 2012 at 5:26
  • 1
    That only works if the hash key doesn't match an existing method name (like Hash#zip, for example). Jan 18, 2013 at 19:54
  • @JoeVanDyk The same limitation applies to OpenStruct. If you had a key-value pair in the hash used to initialize an OpenStruct object, and that key had the same name as an existing method of OpenStruct, it would prefer the method call to returning the hash value. The only difference I see is that OS is standard, and maybe a little faster, and has some other utilities for object management. Using the method_missing way is equally as powerful, and you can extend existing code/libs easily.
    – tralston
    Nov 24, 2013 at 21:37
  • OpenStruct is really slow. stackoverflow.com/questions/1177594/ruby-struct-vs-openstruct/… Plus it clears the method cache. Nov 24, 2013 at 22:29
6

Rather than copy all the stuff out of the hash, you can just add some behaviour to Hash to do lookups.

If you add this defintion, you extend Hash to handle all unknown methods as hash lookups:

class Hash
  def method_missing(n)
    self[n.to_s]
  end
end

Bear in mind that this means that you won't ever see errors if you call the wrong method on hash - you'll just get whatever the corresponding hash lookup would return.

You can vastly reduce the debugging problems this can cause by only putting the method onto a specific hash - or as many hashes as you need:

a={'foo'=>5, 'goo'=>6}
def a.method_missing(n)
   self[n.to_s]
end

The other observation is that when method_missing gets called by the system, it gives you a Symbol argument. My code converted it into a String. If your hash keys aren't strings this code will never return those values - if you key by symbols instead of strings, simply substitute n for n.to_s above.

3
  • 13
    Someone did this early on in a large project I worked on. It caused all kinds of subtle bugs because incorrect method calls on any hash in the system would just return nil instead of alerting us with a NoMethodError. It took forever to remove from the system because code all over the system had come to depend on it. It's one of the most disastrous code-class extensions I've ever seen.
    – Avdi
    Nov 18, 2009 at 3:49
  • 4
    To (mostly) fix that problem, you could do if has_key? n.to_s then self[n.to_s] else raise NoMethodError
    – jtbandes
    Nov 18, 2009 at 3:54
  • Avdi - I'd certainly agree that abuse/overuse of metaprogramming is a problem. There's probably a better way to solve the overall problem (OpenStruct probably) but I just got it working as an exercise. Nov 18, 2009 at 4:04
5

There are a few gems for this. There's my recent gem, hash_dot, and a few other gems with similar names I discovered as I released mine on RubyGems, including dot_hash.

HashDot allows dot notation syntax, while still addressing concerns about NoMethodErrors addressed by @avdi. It is faster, and more traversable than an object created with OpenStruct.

require 'hash_dot'
a = {b: {c: {d: 1}}}.to_dot
a.b.c.d => 1

require 'open_struct'
os = OpenStruct.new(a)
os.b => {c: {d: 1}}
os.b.c.d => NoMethodError

It also maintains expected behavior when non-methods are called.

a.non_method => NoMethodError

Please feel free to submit improvements or bugs to HashDot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.