Ruby Exercises
Fill in the blanks to check your knowledge. Each exercise belongs to a lesson.
Ruby Introduction
Exercise
Print a line.
"Hi"Ruby Get Started
Exercise
Run a script.
hello.rbRuby Syntax
Exercise
Close a block.
if true
puts 1
Ruby Comments
Exercise
Start a comment.
noteVariables and Strings
Exercise
Insert a value in a string.
puts "Hi #name}"
Numbers and Conditions
Exercise
Second condition keyword.
if a else b end
Ruby Variables
Exercise
Instance variable prefix.
name = "x"Ruby Data Types
Exercise
Show the type.
5.Ruby Type Conversion
Exercise
Text to integer.
"7".Ruby Input and Output
Exercise
Show the inspected value.
[1, nil]Ruby Booleans and Comparison
Exercise
Spaceship operator.
1 2
Ruby Symbols
Exercise
Write the symbol for ruby.
:Constants, Globals and Special Variables
Exercise
Environment hash.
["HOME"]Everything is an Object
Exercise
Class of 5.
5.Variables and Constants
Exercise
Constant name style.
= 5Everything Is an Object
Exercise
Class of a value.
5.Numbers
Exercise
Float division.
7.(2)
Strings
Exercise
Interpolate.
"Hi name}"
String Methods
Exercise
Replace text.
s.("a", "b")
Symbols
Exercise
Convert to symbol.
"ok".nil and Truthiness
Exercise
Safe navigation.
x.length
Operators
Exercise
Value assigned only if b is nil.
b ||= puts, print and p
Exercise
Debug print.
xType Conversion
Exercise
String to int.
"5".Ruby Strings In Depth
Exercise
Replace all matches.
s.("a", "b")
Heredocs and Formatting
Exercise
Format a float.
format("%.2f", )
Ruby Numbers
Exercise
Quotient and remainder.
17.(5)
Random and Time
Exercise
Random die roll.
(1..6)Heredocs and format
Exercise
Squiggly heredoc.
<<TXT
Regular Expressions
Exercise
Find all matches.
s.(/a/)
Formatting Numbers
Exercise
Pad with zeros.
"5".(3, "0")
Time
Exercise
Format time.
t.("%Y")
Math and Random
Exercise
Square root.
Math.(16)
Ruby If, Unless and Ternary
Exercise
Run when false.
puts "x" done
Ruby Case
Exercise
Fallback clause.
case x when 1 then 1 0 end
While, Until and Loop
Exercise
Loop until true.
x == 0 do endRuby Iterators
Exercise
Count down.
3.(1) { }
if, elsif, else and unless
Exercise
Opposite of if.
x > 5case Expressions
Exercise
Match clause.
case x 1 then "a" end
Loops
Exercise
Repeat n times.
3. { }
each, map and select
Exercise
Transform items.
a. { |x| x * 2 }
break, next and redo
Exercise
Skip a round.
if i == 2Ranges
Exercise
Exclude the end.
15
Arrays and Hashes
Exercise
Transform each item.
nums. { |n| n * 2 }
Loops and Iterators
Exercise
Repeat 3 times.
3. { puts "hi" }
Ruby Arrays In Depth
Exercise
Remove the last item.
a.Array Methods: map, select, reduce
Exercise
Combine to one value.
n.(:+)
Ruby Hashes In Depth
Exercise
Default for missing keys.
Hash.new()
Ranges and Sets
Exercise
Expand a range.
(1..3).Enumerable and Lazy
Exercise
Mix in the module.
include Arrays
Exercise
Add to the end.
a 5
Array Methods
Exercise
Remove nils.
a.sort_by, min_by, group_by
Exercise
Group items.
w.(&:length)
Hashes
Exercise
Safe lookup.
h.(:x, 0)
Hash Methods
Exercise
Join hashes.
h.(other)
Counting and Tallying
Exercise
Count occurrences.
a.Set
Exercise
Unique collection.
.new([1])Destructuring and Splat
Exercise
Collect the rest.
a, r = [1, 2, 3]
Method Arguments
Exercise
Collect extra arguments.
def f(args) end
Blocks, Yield and Procs
Exercise
Run the block.
def m end
Closures and Method Objects
Exercise
Method as an object.
5.(:+)
Ruby Recursion and Memoization
Exercise
Call the method itself.
def fact(n) = n <= 1 ? 1 : n * (n - 1)
Methods
Exercise
Define a method.
hi; endBlocks and yield
Exercise
Run the block.
Procs and Lambdas
Exercise
Stabby lambda.
sq = (x) { x * x }
Variable Arguments
Exercise
Collect keywords.
def f(opts); end
Method Chaining and Symbols to Proc
Exercise
Shortcut block.
map(&:)
Recursion
Exercise
Base case.
n <= ? 1
Variable Scope
Exercise
Global prefix.
count = 0Methods and Blocks
Exercise
Define a method.
hello
"hi"
endClasses and Modules
Exercise
Auto create getters.
:nameErrors and Files
Exercise
Handle an error.
begin rescue end
Ruby Inheritance
Exercise
Inherit symbol.
class Dog Animal
Modules and Mixins
Exercise
Add instance methods from a module.
WalkablePublic, Private and Protected
Exercise
Hide a method.
def secret; endClass Methods and Constants
Exercise
Define a class method.
def .make; end
Struct, Comparable and to_s
Exercise
Mix in comparison.
include Duck Typing and Polymorphism
Exercise
Test for a method.
x.(:speak)
Classes
Exercise
Constructor method.
def (name)
attr_reader, writer and accessor
Exercise
Getter and setter.
attr :age
Inheritance and super
Exercise
Inherit.
class Cat Animal
Modules and Mixins
Exercise
Add a module.
GreetClass Methods and Variables
Exercise
Class method.
def .n; end
public, private, protected
Exercise
Hide a method.
def x; endto_s, inspect and Comparable
Exercise
Printing hook.
def ; end
Struct
Exercise
Quick class.
.new(:a)Ruby Exceptions
Exercise
Throw an error.
ArgumentErrorRuby File I/O
Exercise
Append mode.
File.open(path, "")
JSON and YAML
Exercise
Read JSON text.
JSON.(text)
Regular Expressions
Exercise
Find all matches.
s.(/\d+/)
begin, rescue, ensure
Exercise
Always runs.
puts "done"Custom Errors
Exercise
Raise an error.
ArgumentErrorReading and Writing Files
Exercise
Write a file.
File.(path, "x")
JSON
Exercise
Parse text.
JSON.(s)
Enumerable and each_slice
Exercise
Mixin for collections.
include Practice: Word Frequency
Exercise
Split on spaces.
text.Practice: FizzBuzz
Exercise
Range of 1 to 15.
(115)
Practice: Simple Bank Account
Exercise
Positive check.
n.?
Metaprogramming Basics
Premium exercise. See plans
Freezing, Immutability and Dup
Premium exercise. See plans
Pattern Matching
Premium exercise. See plans
Threads, Mutex and Fibers
Premium exercise. See plans
Operator Overloading and DSLs
Premium exercise. See plans
Gems and Bundler
Premium exercise. See plans
Testing with RSpec and Minitest
Premium exercise. See plans
Web Apps with Sinatra
Premium exercise. See plans
Ruby on Rails Overview
Premium exercise. See plans
Active Record Basics
Premium exercise. See plans
Command Line Scripts
Premium exercise. See plans
Performance and Style
Premium exercise. See plans
Ruby Best Practices
Premium exercise. See plans
Practice Projects
Exercise
A popular Ruby scraping gem.