Skip to content

Everything in Ruby is an object.

Basic data types

Numbers (integers and floats)

basic arithmetic operators

# Addition
1 + 1   #=> 2

# Subtraction
2 - 1   #=> 1

# Multiplication
2 * 2   #=> 4

# Division
10 / 5  #=> 2

# Exponent
2 ** 2  #=> 4
3 ** 4  #=> 81

# Modulus (find the remainder of division)
8 % 2   #=> 0  (8 / 2 = 4; no remainder)
10 % 4  #=> 2  (10 / 4 = 2 with a remainder of 2)

When doing arithmetic with two integers in Ruby, the result will always be an integer.

17 / 5    #=> 3, not 3.4
17 / 5.0  #=> 3.4

Methods

  • .even?
  • .odd?

Yes, .even? with the ?, like 7.even?.

Strings

Docs

Can be formed with "" or ''. This is known as string literals.

Concatenation

# With the plus operator:
"Welcome " + "to " + "Odin!"    #=> "Welcome to Odin!"

# With the shovel operator:
"Welcome " << "to " << "Odin!"  #=> "Welcome to Odin!"

# With the concat method:
"Welcome ".concat("to ").concat("Odin!")  #=> "Welcome to Odin!"

Substrings

## First character
"hello"[0]      #=> "h"

## First to second
"hello"[0..1]   #=> "he"

## First to fourth
"hello"[0, 4]   #=> "hell"

## Last character
"hello"[-1]     #=> "o"

Escape characters

\\  #=> backslash
\b  #=> Backspace
\r  #=> Carriage return
\n  #=> Newline
\s  #=> Space
\t  #=> Tab
\"  #=> Double quotation mark
\'  #=> Single quotation mark

Interpolation

Allows to evaluate a string that contains placeholder variables.

name = "Odin"

puts "Hello, #{name}" #=> "Hello, Odin"
puts 'Hello, #{name}' #=> "Hello, #{name}"

Methods

#capitalize
"hello".capitalize #=> "Hello"

#include?
"hello".include?("lo")  #=> true
"hello".include?("z")   #=> false

#upcase
"hello".upcase  #=> "HELLO"

#downcase
"Hello".downcase  #=> "hello"

#empty?
"hello".empty?  #=> false
"".empty?       #=> true

#length
"hello".length  #=> 5

#reverse
"hello".reverse  #=> "olleh"

#split
"hello world".split  #=> ["hello", "world"]
"hello".split("")    #=> ["h", "e", "l", "l", "o"]

#strip
" hello, world   ".strip  #=> "hello, world"

Convert other objects to strings

5.to_s        #=> "5"
nil.to_s      #=> ""
:symbol.to_s  #=> "symbol"

Fun with strings

"he77o".sub("7", "l")           #=> "hel7o"
"he77o".gsub("7", "l")          #=> "hello"
"hello".insert(-1, " dude")     #=> "hello dude"
"hello world".delete("l")       #=> "heo word"
"!".prepend("hello, ", "world") #=> "hello, world!"

Symbols

how it differs from a string

:my_symbol

Booleans (true, false, and nil).

true, false and nil.

Everything in Ruby has a return value. When a piece of code doesn’t have anything to return, it will return nil.


Sources


Install

As I have noexec in /tmp I have to create a folder for it to build.

sudo dnf install -y rbenv
mkdir /tmp/build
sudo mount -t tmpfs buildtmpfs /tmp/build
rbenv install -l
TMPDIR=/tmp/build rbenv install 3.1.2
rbenv global 3.1.2
sudo umount /tmp/build
rm -r /tmp/build

irb Interactive Ruby

irb is a REPL (Read-Eval-Print-Loop). Another alternative is pry.

Install it with bundler with gem install irb. Open it with irb.