Better method calling

Oct 12, 2014

I’m very much used to doing this when building a new Ruby class.

class Honey
  attr_accessor :sweetness, :name, :colour

  def initialize(sweetness, name, colour)
    @sweetness = sweetness
    @name = name
    @colour = colour
  end
end

This looks fine but here is a better version.

class Honey
  attr_accessor :sweetness, :name, :colour

  def initialize(options = {})
    @sweetness = options.fetch(:sweetness)
    @name = options.fetch(:name)
    @colour = options.fetch(:colour)
  end
end

Much better. Now we don’t have to initialize Honey objects with a specific order of arguments. Just pass them in as a hash argument and the initialize method does the rest.