top | dibdib: Random things | programming: Ruby  /  EDF  /  Palm  /  Perl | obsessions: Games | about: me  /  my pages  /  people


source (rd documentation)

A Ruby module that performs the same madness as Symbol::Approx::Sub does for Perl -- it lets you call methods approximately.

Consider the following code:

  require "subapprox"

  class Fish
        #include Sub::Approx
        attr_reader :name, :age

        def initialize(age=0, name='')

        def action(exclaim='SHOUT')
                return "#{@name} does action #{exclaim}"

  halibutt =, 'Magarathea')
  print "Fish's name is ", halibutt.nam, "\n"
  print halibutt.actoin("GROOVE")
Notice that we have mispelled two methods: nam and actoin. Normally, this code would cause an error similar to this:
  test.rb:12: undefined method `nam' for #<Fish:0x401777e8> (NameError)
If you include the Sub::Approx module, it installs a method_missing routine that looks through the public methods of the class for one that "matches" the method that is missing. By default, it uses the Soundex module to create the list of matching method names. If the list contains more than one method name, it passes the list off to a function that (by default) returns one at random. The method name is then called with the parameter list, just as if we had called the correct function in the first place, and you get the output you would expect.
  Fish's name is Magarathea
  Magarathea does action GROOVE