Ruby Knowledge

As I Know It

Kip Landergren

(Updated: )

My ruby knowledge base explaining the language philosophy (DRY, everything is an object, duck typing...), syntax, control structures, and implementation.

Contents

Forward

This is an evolving document: I make updates as I learn, build and understand more. All errors are my own!

I have benefited from the work of many people, particularly those who publish what they know. I have listed a non-exhaustive list under Recommended Resources. Consider them!

Language

Philosophy

Programmer Convenience Above All

Ruby tries to get out of the way of the programmer—the lower the barriers between thought and code are, the better.

DRY

Acronym for “Don’t Repeat Yourself”. Good judgment of its application is left to the programmer.

I find this manifests in two distinct ways:

When over applied this can lead to code feeling “magical” because so much of its behavior is implicit or hidden out of sight.

Everything is an Object

A Ruby object may:

In Ruby, all classes are:

There is no equivalent of a primitive or C-style struct. This means that everything you manipulate in code is an object that has state and can receive methods:

ruby-2.5.3> 3.times { puts "we win" }
we win
we win
we win
3
ruby-2.5.3> "we win".upcase
"WE WIN"

including the classes themselves:

ruby-2.5.3> klass = String
String
ruby-2.5.3> klass.is_a? Object
true

Duck Typing

A Ruby type is determined by what actions an object can perform via its methods. There is no defined Type type, or defined interfaces or protocols that an object conforms to. This plays out in code as thinking less about the contract a passed object conforms to (“I have a String, what can I do with a String?”) and more about what the method should perform on a passed object (“I want to append X and then reverse the contents of the passed object”). A TypeError is raised when a receiver’s method cannot execute with the passed object.

Why “Duck”?

From phrase: “If it walks like a duck, talks like a duck, looks like a duck, it is a... duck”. Ruby objects that respond to a common set of messages are considered of the same type.

Are Ruby Classes Types?

No, but a class may implement all methods of a type, also be the sole implementor, and for all intents and purposes be used in interpersonal communication to refer to objects and can perform the actions of a type.

Composition Over Inheritance

Modules

A module is a grouping of functionality—classes, methods and variables—into a scoped structure which cannot be instantiated. Modules provide a namespace for all members and may be included in classes to inject, rather than inherit, functionality.

Single Inheritance

Used when there exists an “is a” relationship between classes. A subclass is a strict specialization of the super class. Subclasses follow the Liskov Substitution Principle.

Mixin

A module is included—“mixed in” to a class—when there exists a “has a” or “uses a” relationship between the class and module. A mixin provides functionality without incurring the tight coupling that subclasses can.

Syntax

Variables

ruby-2.5.3> f = "foo"
"foo"
ruby-2.5.3> f, b = "foo 2", "bar"
["foo 2", "bar"]
ruby-2.5.3> f
"foo 2"
ruby-2.5.3> b
"bar"

Expressions

ruby-2.5.3> a = 2
2
ruby-2.5.3> b = a * 3
6

Classes

ruby-2.5.3> class Foo; end
nil
ruby-2.5.3> f = Foo.new
#

Methods

ruby-2.5.3> def square(x)
  x * x
end
:square
ruby-2.5.3> square(4)
16

Modules

ruby-2.5.3> module Talk
  def talk
    puts "hi there!"
  end
end
:talk
ruby-2.5.3> class Parrot; include Talk; end;
Parrot
ruby-2.5.3> p = Parrot.new
#
ruby-2.5.3> p.talk
hi there!
nil

Control Structures

Loops

loop do; end
ruby-2.5.3> i = 0; loop do
  puts "i is: #{i}"
  break if i == 3
  i += 1
end
i is: 0
i is: 1
i is: 2
i is: 3
nil
for loop
ruby-2.5.3> for i in 0..3
  puts "i is: #{i}"
end
i is: 0
i is: 1
i is: 2
i is: 3
0..3
while
ruby-2.5.3> i = 0; while i <= 3
  puts "i is: #{i}"
  i += 1
end
i is: 0
i is: 1
i is: 2
i is: 3
nil

Conditionals

ruby-2.5.3> for i in 0..3
  puts "#{i} is odd" if i % 2 != 0
end
1 is odd
3 is odd
0..3
ruby-2.5.3> for i in 0..3
  puts "#{i} is odd" unless i % 2 == 0
end
1 is odd
3 is odd
0..3

Core Library

Official Core Library Documentation

Standard Library

Official Standard Library Documentation

Implementation

Ruby does not have a language specification, instead it has a canonical implementation in YARV (Yet Another Ruby VM).

YARV

How Code Gets Executed

Ruby is an interpreted language.

Optimizations

There are no primitives, but some objects are implemented as immediate value types.

Ruby uses automatic memory management.

Dynamically Typed

Caveat: I do not know how YARV actually treats types behind the scenes—it may do nothing!

“Dynamic” refers to the deferring of type checking to runtime. Ruby variables and method arguments have no associated, checked or enforced type. At runtime the objects the variables reference determine the behavior of code, and only through their ability to respond to methods.

Strongly Typed

Ruby core classes employ the following type conversion conventions:

Ruby makes no requirement to follow these in your own classes, and outside of these core classes will execute no implicit or indirect type conversions. Type conversion of your own objects only occurs based on what you define their methods to do. Because of this Ruby is considered “strongly typed”.

Loose Conversion

to_x, where “x” is the first letter of the resulting class’s name. Used when the receiving object should be represented as another class.

Strict Conversion

to_xyz, where “xyz” is the short-form representation of the resulting class’s name. only implemented if the receiving object can be substituted everywhere the resulting class object could be used and still maintain the receiver’s original type duties. Used by a subset of classes in the core library for the language’s only implicit type conversion rules via try_convert.

Numeric Conversion

Method coerce takes a single input parameter and returns:

The implementation of the coerce methods resolve the question of what the resulting type of an arithmetic operation will be.

Recommended Resources

This is a partial and unranked list of resources I have derived great benefit from: