Data Types

  • Numbers - You know, the ususal. (40, 1.3, 6969696…)
x = 3
y = 2.9
  • Strings - Again, do we need to go over this? (“Steve”, “is”, “dumb”…)
name = "steebe"
  • Booleans
5 < 10          # => true
5     <= 10     # => true
'abc' == 'def'  # => false
true            # => true
123456          # => true
0               # => true
nil             # => false
'xyz'.empty?    # => false
'a' > 5         # => error:
                # comparison of String with
                # Numeric failed
  • Arrays - A list of items whose initial value is denoted with brackets ([]) (index access similar to Python => arr[0] returns the first item of arr)
arr = [1, 2, 3]
  • Hashes - A map; a list of key value pairs (similar to Python dict)
h = {"key" => "value", "name" => "steebe"}
  • Symbols - A static constant of sorts. The syntax of a symbol in code is _:_ (:symbolA, :mysymbol, :etc). The symbol can be reused over and over, and memory is only allocated once for its representation. Its underlying representation, by the way, is a string when it is consumed.
puts :mySym # => mySym


In Ruby, any code contained within curly braces ({}) is considered a block. Blocks are special, because they are often invoked by a function. The invoker is responsible for sending any values to the block in the form of a variable. For example:

5.times { |time|    # start of block; value assigned to time by times
    puts time       # prints 0 -> 5

Note that the variable is passed into the block in a parameter list encapsulated in pipes. Any multi-parameter implementation is comma separated.

String Formatting

In the context of processing strings or printing to them to the console, Ruby offers a convention that helps with formatting and dealing with expressions as strings.

Within double quotes, the hash sign followed by a pair of curly braces denotes accessing a value. The contents within the curly braces must be a variable or expression that represents a string.

player = {"name" => "steebe", "age" => "24", "game" => "GOW"}
player.each { |k, v|
    puts "#{k}, #{v}" # => name, steebe \n, etc.


Functions (inherently “methods” due to being defined within a class body) can be called from any object in Ruby, but only certain methods work on certain objects. Functions can require parameters, and upon invocation optional parameters denoting the argument list.

x = "Hello!"
y = x.gsub("lo", "p")
z = x.gsub "lo", "p"
puts y      # => Help!
puts (z)    # => Help!

Out of the Box Functions

Function Caller Type(s) Notes
puts System Put “arg” prints its argument to the screen followed by a newline.
print System Prints its argument to the screen.
.times Number Iterates for a cycle whose count is n, the number being invoked on. If followed by a block, will execute that block n times.
.length String Prints the length of a string. Does not work on numbers!
.gsub(arg1, arg2) String “Global Substitute” replaces all instances of arg1 in the String with arg2.
.lines String Converts a string to an array of its contents. Each item represents a block of characters that are followed by a newline.
.ljust(intArg) String When called on a string, justifies the string to the left side of the buffer with its argument specifying the amount of padding.
.reverse String, Array Reverses the attributes of an object. Does not work on numbers!
.max Array Prints the greatest item of an array of numbers.
.sort Array Sorts the contents of an array of numbers.
.join(delimiter) Array Joins the contents of an array with the delimiter as the separators between each item.
.each Array, Hash Returns the contents of an array or the key/value pairs of a hash.
.select Array, Hash Returns the contents of an array or hash filtered by a block.
.count Array, Hash Returns the amount of elements in an array or hash.
.keys Hash Prints the keyset of a Hash as an array.
.to_s   Attempts to convert a non-string to a string.
.to_i   Attempts to convert something to an integer.
.to_a   Attempts to convert something to an array.


As with any robust high level language, Ruby allows for the developer to create their own methods. To create a method in Ruby, the def and end keywords are required to form a block.

def myMethod (arg1)
    # method body


A custom method does not include its return type in its stub. Instead, it just returns what it must when it must. The return statement can include the return keyword, or it can simply be the value that is being returned. In other words, the return keyword is optional.

Bang! (!)

numbers = [3, 5, 1, 4, 66, 2]
numbers   # => [3, 5, 1, 4, 66, 2]
numbers   # => [3, 5, 1, 4, 66, 2]
numbers   # => [1, 2, 3, 4, 5, 66]

Helpful sources