Hi, I'm Nicholas Johnson!

software engineer / trainer / AI enthusiast

Method Missing

Trying to call a method that doesn’t exist in Ruby is an exception rather than a language error. We can catch it, or we can simply implement a method_missing function which will be called when no matching method is found.

  class A

    def ary

    def method_missing(method, *args)
      puts ary.include?(method)

  a = A.new


Exercise - Create a Spaceship.

Assuming you have built a mighty warship, give your class an array of abilities like this: [:warp, :photon_torpedos, :holodeck] etc.

Now we’re going to use method_missing to allow us to query our space ship. We will be able to call methods like enterprise.has_holodeck?, and voyager.can_warp? and get back a true or a false value.

Here’s a regex that should help

  FEATURE_REGEX = /^(?:has|can)_(\w*)\?$/
  if find = method.to_s.match(FEATURE_REGEX)
    feature = find[1]

Real world example

For a real world example, read and understand the string_enquirer codebase here: