Method Driven Development

March 23, 2018

I’ve created a screencast to show you a technique I sometimes use. I call this approach “Method Driven Development” (MDD).

(I’m sure many people use this technique, though I don’t know if there’s a name for it.)

Using MDD, I explore the Geocoder gem with the goal of finding the distance between Toronto and Chicago.

After watching this video, you’ll understand the ins and outs of MDD, including how to:

  • investigate the methods that are available on each Ruby object
  • sift through those methods and quickly find the method you want
  • pull up docs on the method without leaving pry
  • use Ruby’s grep method

Plus, you’ll also learn the basics of the geocoder gem: searching, distance between, working with kilometeres and miles.

Ready to give it a try? Install the interesting_methods gem I’ve just released and then code along as you watch this video.



Update:

There’s a few thoughtful pry tips on a reddit thread for this screencast:

https://www.reddit.com/r/ruby/comments/86vncl/method_driven_development_with_the_new/

Most useful is that pry has an ls command to show just the useful stuff about an object.

Let’s say you have an Animal class that speaks:

class Animal
  def speak
    puts 'Moo'
  end
end

Meanwhile in pry:

pry> ls Animal
Object.methods: yaml_tag
Animal#methods: speak

Here’s what Rails looks like:

pry> ls Rails
constants:
  Application  Configuration  ConsoleMethods  Engine      Html  InfoController  LineFiltering      Paths  Railtie  TestUnit         VERSION
  Command      Console        Dom             Generators  Info  Initializable   MailersController  Rack   Secrets  TestUnitRailtie  WelcomeController
#<Module:0x00007faa75953720>#methods: reachable?
ActiveSupport::Autoload#methods: autoload  autoload_at  autoload_under  autoloads  eager_autoload  eager_load!
Rails.methods:
  app_class   application   backtrace_cleaner  cache=         env   gem_version  initialize!   logger   public_path  version
  app_class=  application=  cache              configuration  env=  groups       initialized?  logger=  root
instance variables: @_at_path  @_autoloads  @_eager_autoload  @_env  @_under_path  @app_class  @application  @cache  @logger

Enjoy!