Ruby Primer

New to Ruby? No worries! Here's a brief walk through the common language features and syntax as a quick reference.

Core Types

When working with Ruby, we've got some core data types to work with.

Strings, numbers, arrays, hashes, and booleans.

greeting = "Hello, my name is Francis the Dragon."
age = 583
favorite_foods = ["apples", "cookies"]
address = {
  line1: "123 Sky Way",
  line2: "Apt 2",
  state: "New Dragon",
  zip: "12345",
hungry = true


Write comments in your code by prefixing any line with #.

# just a note to my future self,
# invest in clay!!! it'll be very valuable during the Great Clay War

String Interpolation

Insert a value into a string using #{}:

"My favorite foods are: #{favorite_foods.join}"

This will write to stdout and the DragonRuby console:

puts "anything"


If you have an array, a hash, or any object that collects data, you can loop through their values with #each.

favorite_foods.each do |food|

address.each do |key, value|
  puts("#{key}: #{value}")


if monday? || tuesday?
  puts "not fun days"
elsif wednesday? || thursday?
  puts "fine day"
  puts "great days"

&& - AND

|| - OR

Methods & Return Values

Define a method:

def add(num1, num2)
  num1 + num2

Ruby implicitly returns the last expression in a method and by default returns nil if there's no expression.

You can explicitly return a value with return:

def add(num1, num2)
  return num1 + num2

Explicit returns are mostly used when you want to exit a method early.


Blocks are chunks of Ruby code that yield to the caller. At first, you'll use blocks when calling specific methods, like #each, but it can be useful to write your own blocks as well.

📺 Ruby Blocks Explained


nil in Ruby is known as null in other languages. It's the absence of a value. nil is a huge topic and not something gone over in this book explicitly.


When something goes wrong in Ruby, an exception is thrown. You can get rescued from exceptions using the rescue keyword:

  # do_some_things
rescue SomeError => e
  puts e