Amokrane
5/22/2011 - 12:52 PM

Pair your method_missing with a respond_to?

Pair your method_missing with a respond_to?

# This snippet is taken from:
# "The Polite Programmer's Guide to Ruby Etiquette" 
# http://bit.ly/hrwdPY

class BlackMagicStereo < Stereo
  def method_missing(name, *args)
    if sym.to_s =~ /^play_(\w+)$/
      system("open songs/#{$1}.m4a")
    else 
      # Here, very important! 
      # Don't forget to delegate up to previous implementation
      super 
    end
   end
end

def respond_to?(name)
  sym.to_s =~ /^play_/ || super 
end