1

I have a Ruby-Cucumber framework. I need to use the method method_missing to avoid writing a few repetitive functions. Where should I place my method_missing? In any .rb file under the support folder? Or should it go in some different specific file?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Aks..
  • 1,343
  • 1
  • 20
  • 42
  • Try here: Post is titled: "Override method_missing for class and instance methods?" and you might get some ideas of how to do it. http://stackoverflow.com/questions/22846777/override-method-missing-for-class-and-instance-methods – BenKoshy Aug 31 '16 at 09:38

1 Answers1

1

Define method_missing like you would any Cucumber helper method, in an .rb file in the support directory and in a module which you then pass to the Cucumber World:

module MethodMissing
  def method_missing(method)
    puts "You said #{method}"
  end
end
World MethodMissing

You can then call missing methods in step definitions.

Like any Cucumber helper method, method_missing should be defined only on the Cucumber World. If you defined it at the top level of the support file, it would be defined on the Ruby top-level object and available everywhere, which is unparsimonious and might break other code.

I deliberately didn't defer to super, since the World doesn't define method_missing, or define respond_to_missing?, since I didn't have any plans to call method(:foo) on the World, but you can do those things if you prefer to always do them when you define method_missing.

Community
  • 1
  • 1
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121