Checkpoint: Ruby Basics

A checkpoint is a consolidation lesson that pulls together everything you've learned so far — operators, truthiness, loops, case/when, string methods, enumerable, Struct, and Set — into one realistic build challenge that proves the pieces fit together.

Learn Checkpoint: Ruby Basics in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

There's a recap, a multi-step program to build (with a full worked solution to compare against), and a short quiz to confirm the ideas have stuck.

What You've Learned So Far

🛠️ Build Challenge: A Word-Frequency Report

Your mission is a single script that reads a sentence and prints a tidy report: a bar chart of word counts (built from a Struct and tally ), the repeated words (via a Set ), and a case/when verdict on the vocabulary size. Start from the brief below.

One clean way to solve it — note how each numbered step maps to a lesson you've completed:

Touched lessons: String methods (downcase/split/ljust), Enumerable (tally/map/sort_by/select), Struct (WordStat), Operators (ternary, unary minus for the sort key), Sets (to_set), and case/when (the verdict).

📝 Checkpoint Quiz

Answer each in your head (or out loud), then expand to check. If you miss one, the parenthetical points you to the lesson to review.

Only nil and false . Everything else — including 0 , "" , and [] — is truthy, so if 0 runs its block.

It's 2 — integer division truncates. For 2.333… make a side a Float: 7.0 / 3 or 7.fdiv(3) .

It uses case-equality: when_value === subject . That's why ranges ( 80..89 ), classes ( Integer ), and regexes all work as when targets.

map returns a new array the same size, transforming each element. reduce collapses the whole collection into a single value via an accumulator, e.g. [1,2,3].reduce(:+) is 6 .

A Set rejects duplicates and uses hashing, so include? is roughly O(1). On an Array, include? scans every element (O(n)), which is slow inside loops.

"ab" * 3 is "ababab" (string repetition). gsub replaces every match while sub replaces only the first.

Practice quiz

Which values are FALSY in Ruby?

  • nil, false, 0, and ""
  • Only false
  • Only nil and false

Answer: Only nil and false. Only nil and false are falsy. Everything else, including 0, "", and [], is truthy, so 'if 0' runs its block.

What does 7 / 3 evaluate to in Ruby?

  • 2
  • 2.333
  • 3
  • 2.0

Answer: 2. Integer division truncates, so 7 / 3 is 2. For the decimal, make a side a Float: 7.0 / 3 or 7.fdiv(3).

How do you get the decimal result of dividing 7 by 3?

  • 7 / 3
  • 7 % 3
  • 7.div(3)
  • 7.0 / 3 or 7.fdiv(3)

Answer: 7.0 / 3 or 7.fdiv(3). Make one operand a Float (7.0 / 3) or use 7.fdiv(3) to force floating-point division and get 2.333...

What does a 'when' clause actually compare with under the hood?

  • == (equality)
  • === (case-equality), i.e. when_value === subject
  • eql?
  • equal?

Answer: === (case-equality), i.e. when_value === subject. case/when uses ===, so ranges (80..89), classes (Integer), and regexes all work as when targets.

What is the difference between map and reduce?

  • map returns a same-size array; reduce collapses to one value
  • They are identical
  • map collapses to one value; reduce transforms each element
  • Both return a single number

Answer: map returns a same-size array; reduce collapses to one value. map transforms each element into a new same-size array; reduce folds the collection into a single value, e.g. [1,2,3].reduce(:+) is 6.

Why pick a Set over an Array for membership checks?

  • Sets preserve insertion order
  • Arrays cannot use include?
  • Set rejects duplicates and uses hashing, so include? is roughly O(1)
  • Sets are always smaller

Answer: Set rejects duplicates and uses hashing, so include? is roughly O(1). A Set hashes its elements, so include? is about O(1). On an Array, include? scans every element (O(n)).

What does "ab" * 3 produce?

  • "ab3"
  • "ababab"
  • "ab ab ab"
  • An error

Answer: "ababab". String * repeats the string, so "ab" * 3 is "ababab". The challenge uses "*" * count to draw a bar chart.

What is the difference between gsub and sub on a String?

  • gsub replaces only the first match
  • They are identical
  • sub returns an array
  • gsub replaces EVERY match; sub replaces only the FIRST

Answer: gsub replaces EVERY match; sub replaces only the FIRST. gsub (global sub) replaces every match, while sub replaces only the first occurrence.

In the build challenge, which Enumerable method counts how many times each word appears?

  • group_by
  • tally
  • reduce
  • sort_by

Answer: tally. tally returns a hash mapping each element to its count, e.g. words.tally turns the word list into word => count.

How do you sort the WordStat list by count DESC then word ASC with sort_by?

Negating the count (-s.count) sorts it descending while word stays ascending, all in one array sort key.