Introduction To Ruby
If As A Modifier

Ruby is full of cute and interesting ways to make code more natural to write. The following program shows an alternative way to write an if statement.

# If as a modifier

x = 5

puts "x is 5" if x==5

gets

You cannot write an else clause with this method.

Challenges

  1. The following table shows a list of ages and one of the legal rights that a person gets when they reach that age. Use this information to write a program where the user can enter their age and see a list of the legal rights they have. Write this program using a series of statements like the above example.
    • 12: See a 12A-rated film at the cinema without an adult.
    • 14: Have a job which can be described as 'light work'.
    • 15: See a 15-rated film at the cinema without an adult.
    • 16: Enter a pub on your own. You cannot buy alcoholic drinks.
    • 17: Donate blood.
    • 18: Serve on a jury.
    • 21: Adopt a child
  2. Look through any other programs you have written with if statements. Try to refactor them so that they use the modifier approach.