Method ref in menu

Ruby 1.91 on Debian 7.3 running on Pentium 3
trying to write menu like so:
def foo; end
def bar; end
mHash={‘f’=>[‘foo’,foo], ‘b’=>{‘bar’,bar]}
def menu(mH)
mH.each_key { |k| puts k + ’ ’ + mH[k][0] }

puts 'choice? '
 if mH.has_key?( ch )


Not really knowing what I’m doing yet I’ve tried various sigils both in the hash definition and the execution call with no joy. Is 1.9.1 too old to have this functionality or have I just not found it yet?
I did find a posting by Jim Weirich of reference.rb but it didn’t fly here.
I’m really just looking for info since I’ve already rewritten menu using a case/when/then but it’s really clunky.
PS: ‘Stay at home’ is a good time to explore another language isn’t it?

Object#method is probably what you want: (it appears to have been in 1.9.1, according to my PickAxe book!).

> def foo; puts "in foo"; end
 => :foo 
> hash = {'f' => method(:foo)}
> hash['f'].call
in foo
 => nil 

P.S. have fun with Ruby!

Worked perfectly, many thanks.
Blessings on your house.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs