×

ruby · level 2 · unit 7 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A keeps related tools together under one name.

Some methods belong together but need no objects, like money helpers. Put them in a module: module Money, the methods, then end. Write each method as def self.pounds(pence), and call it with the module's name: Money.pounds(250).

A is a value that should never change, written in capitals, like VAT = 0.2. Reach a module's constant with two colons: Money::VAT. Ruby's own Math module works the same way, with Math.sqrt(16) and Math::PI.

module Money
  VAT = 0.2
  def self.pounds(pence)
    format("£%.2f", pence / 100.0)
  end
end
puts Money.pounds(250)
puts Money::VAT
puts Math.sqrt(16)
puts Math::PI.round(2)

self. in front of a method's name means the method belongs to the module itself.