0 Replies - 1317 Views - Last Post: 15 November 2013 - 02:35 PM

#1 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

Hash Dispatch

Posted 15 November 2013 - 02:35 PM

Description: Pass the name of any method as a symbol to the hash, and it'll dynamically dispatch to the method. Works best in a class or module, but best not to use in production code. More of an interesting thing you can do.

[1] pry(main)> call = Hash.new{ |h,k| h[k] = self.send k }
=> {}
[2] pry(main)> call[:puts]

=> nil
[3] pry(main)> call[:inspect]
=> "main"
Exploiting Hash Default Values for a method dispatcher
call = Hash.new{ |h,k| h[k] = self.send k }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1