ruby · level 2 · unit 7 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Ruby comes with a toolbox. Gems are tools others share.
You have used json already. It is part of the : tools that come with Ruby, ready for you to require. gives you Set, a collection that never holds the same item twice. gives you Date, which knows the calendar.
A is a package of Ruby code that someone else wrote and shared. On your own computer you install gems with the gem install command, then require them. Gwecode runs Ruby in your browser with no gems, so these lessons use the standard library only.
require "set"
require "date"
seen = Set.new(["fox", "owl", "fox"])
seen << "owl"
p seen.size
day = Date.new(2026, 12, 25)
puts day.strftime("%A %d %B %Y")
puts day + 7
gap = day - Date.new(2026, 12, 1)
puts gap.to_i strftime works a bit like format, with %A for the weekday and %B for the month.
guess first
No doubles allowed. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
require "set"
stickers = Set.new
["star", "cat", "star", "moon", "cat"].each { |sticker| stickers << sticker }
p stickers.size
p stickers.include?("moon")
your turn
Your turn. Count down to a big day.
Fill in a month and a day in 2027, as numbers. The program counts how many days that is after New Year.
fix it
Christmas will not fit in the calendar. Fix it!
- Read the error. Ruby says the date is not valid. In what order does
Date.newwant its numbers? Date.newtakes the year first, then the month, then the day.- Change line 2 to xmas = Date.new(2026, 12, 25), then press run.
nailed it
you know where the tools come from. :D
- The standard library comes with Ruby.
requireloads it. - A Set never holds the same item twice.
- Gems are shared packages you install on your own computer.
new words in your collection
- standard library
- Set
- Date
- gem
This lesson isn’t saved. A free account remembers every lesson you finish.