Rails Noob's Notes

Because my memory is good but short.

Ruby

Parameters made of hash

Using fetch with default values.

def initialize(args)
  @health = args.fetch(:health, 10)
  @mana = args.fetch(:mana, 5)
  @attack = args.fetch(:attack, 5)
  @defense = args.fetch(:defense, 5)
end

Using merge and defaults methods.

def initialize(args)
  args = defaults.merge args
  @health = args[:health]
  @mana = args[:mana]
  @attack = args[:attack]
  @defense = args[:defense]
  def defaults
    { health: 10,
      mana: 5,
      attack: 5,
      defense: 5 }
  end
end