Even better method calling

Oct 18, 2014

With Ruby 2.0 there's now an even better way to initialize objects (or call methods) which uses keyword args. We can now do this:

class Apiary < ActiveRecord::Base

belongs_to :site

def honey_production(range:, manuka: false)
  hives.each do |hive|
    hive.honey_for_range(range, manuka)
  end
end

# Call the method like so
site.honey_production(range: two_week_range, manuka: true)

Rather than having to extract the method args from an options hash, just use the args directly. Nice and tidy. Reordering args in the call is fine since the method will sort the args as needed.