Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

LESSON: ruby



By the end of this, developers should be able to:

  • Run Ruby code using Ruby REPL and interpreter.
  • Identify basic language features and types in Ruby.
  • Write a fizzbuzz script in Ruby.
  • List and use common operators in Ruby.
  • Identify operators in an expression and explain what they do.


  • pry: ruby REPL and debugger
  • irb: ruby REPL (but we will use pry, mostly)
  • specs: tests
  • rspec: the test library we will use to write and run tests in ruby
  • implicit return: when a return keyword is not needed because the method returns the last line in the method. Whatever is evaluated last is returned to the caller.
  • puts: how we console.log in ruby (shortened to p)
  • elsif some_condition: how we write else if (someCondition) { ... some js code ... } in ruby
  • snake_case: convention for naming things with multiple words (as opposed to camelCase)


  • Notice what is different between Ruby and Javascript
    • variables do not need let or const
    • Ruby doesn't have an increment operator, either pre (++i) or post (i++). Use += instead.
  • More notes in the lesson's
  • To see all the methods that strings have in Ruby, open up pry, type a string followed by a '.', and hit tab; alternatively, you can call "some string".methods.sort for a full list. And, of course, the Ruby documentation has a full list as well.

Differences between Ruby and JS

  • In Ruby, variables can be simply defined, without previously being declared. This means that with Ruby, we don't need keywords like let and const before variables. We can simply declare the variable and assign it a value variable = value.


a = 1

returns 1

- **String interpolation** requires double quotes ( " my string " )

# without string interpolation:

[1] pry(main)> name = "Lauren"
=> "Lauren"
[2] pry(main)> height_in_feet = 5
=> 5
[3] pry(main)> name + " is " + height_in_feet.to_s + " feet tall."
=> "Lauren is 5 feet tall."

# With string interpolation:

[1] pry(main)> name = "Lauren"
=> "Lauren"
[2] pry(main)> height_in_feet = 5
=> 5
[4] pry(main)> "#{name} is #{height_in_feet} feet tall."
=> "Lauren is 5 feet tall."

  • A Ruby if condition looks quite similar to a JavaScript if. Some of the major differences are:

  • In Ruby, we use elsif, not else if.

  • Conditions don't require parentheses (though they can still accept them).

  • No curly braces required. Simply break up your condition from your code with a newline (as above), a semicolon, or the keyword then (e.g. if .... then).

  • The end of the if is indicated by the keyword end. end is an extremely common keyword in Ruby, appearing at the end of pretty much any contiguous section of code. (kind of like a closing curly brace in javascript, an end matches opening keywords like class, if and def)

  • The do ... end is a common construction in Ruby because it specifies what's known as a block, a grouping of several lines of code.

  • Ruby methods use an implicit return - by default, they will return the value of the last expression evaluated (which may or may not be a return expression). However, Ruby does also have a return keyword which, as it does in JavaScript, immediately terminates the function/method and sends back a value

  • objects must define key/value pairs with bracket notation. Dot notation will be interpreted as calling a method on an object.

  • == and === mean different things between the two languages. In JavaScript, === is a 'strict equality' comparator, while == is a 'loose equality' comparator; since == has some weird exceptions, the convention is to almost always use ===. In Ruby, however, the reverse is true; you should == to test for equality, and not use === (which does something different).

  • Use .equal? if we need to test for identity (two variables that reference the same object).

  • Ruby has several different numeric types (unlike JavaScript), but most operations "do what we expect".

  • Booleans: Only false and nil are falsy in Ruby. Everything else is truthy.

  • We don't need to use parentheses when invoking a method (as we saw above with the + method - one exception later). But sometimes they add clarity, so it can be beneficial to include them.

  • The Ruby comment character is #. Everything following a # on a line is ignored by the interpreter.

  • in JS, we used console.log to print a message to console. Ruby it is common to p or puts a messasge to the console.

  • Ruby's convention is to use underscores between words in names (a.k.a. 'snake_case'). Constants start with a capital letter.

  • Ruby doesn't implicitly convert numbers to strings.

Similarities between Ruby and JS

  • arrays
  • run ruby programs similar to node, like ruby my_ruby_file.rb