Author: Robert Hopman
Last updated: 18-Aug-2025
Table of contents- Ruby syntax
- Read documentation
- Debugging
- Literals
- Blocks
- Variables
- Control Flow
- Methods
- Classes
- Collections
- Inheritance
- Modules
- Exceptions
- Input and Output
- Concurrency
- Testing
- Command-line Tool
- Ruby Gems
- Interactive Ruby
- Web
- Ruby Style
- Typed Ruby
- General Rules
- References
Language consists of a system:
- Semantics studies the aspects of meaning.
- Syntax studies the structure, principles and relationships.
Read documentation
- Documentation is a representation of the language (im)possibilities.
- Documentation can be found at
- https://docs.ruby-lang.org/en/master/index.html
- https://docs.ruby-lang.org/en/master/table_of_contents.html
- https://rubyreferences.github.io/rubyref/
- https://rubyapi.org
- via ruby info ri command in terminal, e.g. ri Array#map or ri .map
- via help command in interactive ruby. First irb then help Array#map or help .map
- ruby-lang.org search shows more results from the documentation, rubyapi.org shows less results.
- Class methods are called on the class itself and are defined with self. Instance methods are called on an instance of a class and are defined without self.
- :: is a scope resolution operator. It is used to reference a constant, module, or class defined within another class or module. It is documented as a class method.
- # is a method call operator. It is documented as an instance method.
- most documentation is generated by RDoc https://github.com/ruby/rdoc, and the superset: YARD https://github.com/lsegal/yard
This documentation uses the following syntax conventions sometimes:
Debugging
To debug in a console we can use a debugger: debug.
Set a breakpoint in the code with binding.break or the alias debugger.
Run the program and it will enter the console at that point.
Control flow inside the debugger:
- s (step) to step into the next line of code
- n (next) to step over to the next line in the current file
- c (continue) to continue execution until the next breakpoint
- b will show the current breakpoints
- q (quit) to exit the debugger
How to get to the debugger step by step?
For debugging use p instead of puts:
- p (print the value of the expression, including the value of the expression)
- pp (pretty print the value of the expression)
- print (prints without trailing newline)
- puts (prints expression, appends newline and returns nil)
As example:
nil | nil | nil | ||
“test” | test | “test” | test | “test” |
:test | test | :test | test | :test |
[1, 2, 3] | 123 | [1, 2, 3] | 1\n2\n3 | [1, 2, 3] |
{:a=>1, :b=>2} | {:a=>1, :b=>2} | {:a=>1, :b=>2} | {:a=>1, :b=>2} | {:a=>1, :b=>2} |
[1, {1=>”a”}] | 1{1=>”a”} | [1, {1=>”a”}] | 1\n{1=>”a”} | [1, {1=>”a”}] |
Next to the official debugger, there is the Pry gem https://github.com/pry/pry with https://github.com/deivid-rodriguez/pry-byebug to get step by step debugging control.
Some interesting commands for navigation in Pry:
Debugging resources:
- https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer/
- https://www.schneems.com/2016/01/25/ruby-debugging-magic-cheat-sheet.html
- https://github.com/AndyObtiva/puts_debuggerer
- https://www.schneems.com/2016/01/25/ruby-debugging-magic-cheat-sheet.html
- https://dev.to/st0012/optimize-your-debugging-process-with-object-oriented-tracing-and-tappingdevice-39c6
- https://github.com/st0012/object_tracer
- https://github.com/ruby/tracer?tab=readme-ov-file#objecttracer
- https://st0012.dev/my-ruby-debugging-tips-in-2025
- https://st0012.dev/ruby-debug-cheatsheet
- https://github.com/MadBomber/debug_me
- https://www.justinppearson.com/projects/ruby-tutorial.html
Reserved keywords
Keywords- Version: 3.3
- With definition: https://ruby-doc.org/docs/keywords/1.9/
Literals
https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html
A literal is any notation that lets you represent a fixed value in source code (wikipedia).
What does that even mean?
The notation is it’s direct or literal value: e.g. 1 is a literal for the number 1, "hello" is a literal for the string "hello", [1, 2, 3] is a literal for the array [1, 2, 3]. It’s a constant or fixed value, it doesn’t change. (e.g. 1 is always 1, "hello" is always "hello", [1, 2, 3] is always [1, 2, 3]). It’s the primary way to introduce values into a program.
This is different from the indirect or variable notation: x = 1 where x is a variable which refers to 1, which can change. Or for Constants FOO = 1, Expressions (1 + 2), or Methods def foo; puts 1 end which all have indirect or computed values.
Basic literals:
Regular Expression - Regex
A regular expression (also called a regex or regexp) is pattern that can be matched against a string. It is a way of specifying a set of characters that matches a string or part of a string. It is a match pattern (also simply called a pattern). Regex can be used for pattern matching and pattern replacement. Specific patterns can be defined with: Anchors, word boundaries, character classes, repetition, alternation and grouping.
The following are metacharacters with specific meaning: . ? - + * ^ \ | $ ( ) [ ] { }
https://docs.ruby-lang.org/en/master/Regexp.html#class-Regexp-label-Special+Characters
Operater =~ returns characters offset of beginning:
!~ is the negative match operator, which returns true if the string does not match the pattern:
Changing strings with patterns: .sub, .gsub, .sub!, and .gsub!. Sub is for the first match, gsub is for all matches.
Regex has modifiers, with the x at the last example below, you can add newlines, whitespace and comments inside to make it more readable:
After a succesful match via Regexp#match or =~ it returns a MatchData object, which is a collection of information about the match: https://docs.ruby-lang.org/en/master/MatchData.html
Numbers
Ruby supports integers, floating-point, rational and complex numbers. Intergers are assumed to be decimal base 10, but can be specified with a leading sign, as base indicatar: 0 for octal, 0x for hexadecimal and 0b for binary (and 0d for decimal), followed by a string of digits in the appropriate base.
BigDecimal is Ruby’s high-precision decimal number class.
Rational numbers are the ratio of two integers (they are fractions) and therefor have an exact representation:
Complex numbers represent points on the complex plane, and have 2 components: the real and imaginary parts.
Looping using Numbers
Strings
Ruby strings are sequences of characters and instances of class String.
Usually strings are created using string literals - sequences of characters between single or double quotes (delimiters). How the string literal is created, defines the amount of processing that is done on the characters in the string.
Escaping characters inside single-quote is a form of processing:
Double-quoted strings support:
- many escape sequences, e.g. \n the newline character.
- string interpolation, which means you can use any ruby code into a string using #{ expression }.
- global, class or instance variables: #$foo, #@@foo or #@foo.
Not recommended:
Produces: now is the time for all bad coders...
Some style guides prefer single quotes, if interpolation isn’t used, because they are faster.
Syntax to create a string literal can also be as follows, with any nonalphanumeric or nonmultibyte character:
Finally, you can construct a string using a here document, or heredoc.
Type conversion:
Encoding
Encoding is a mechanism for translating bits into characters. For many years, most developers who used English used ASCII, a 7-bit encoding of English characters, such as binary 101 to capital A. Later, an 8-bit representation called Latin-1 that included most characters in European languages became common. All of these were superseded by Unicode, a global standard for all text characters used in all languages: https://home.unicode.org/
Struct
A Struct is a class that is used to create objects that have attributes.
Ranges
Ranges represent a range of values. Ruby uses ranges to implement sequences and intervals.
Blocks
A code block is a chunk of code that can be passed to a method. You can think of a block as somewhat like the body of an anonymous method, as if the block were another parameter passed to that method. Usually between braces on one line and do/end when block spans multiple lines. Parameters to a block are separated by commas, and they are always local to the block. You can define block-local variables using the ; character in the block’s parameter list.
The act of doing something to all objects in a collection is called enumeration in Ruby; in other languages it is called iteration. e.g. each, find, map, sort_by, group_by, map, reduce.
Ruby remembers the context of an object, local variables, block, and so on, this is called binding. Within the method, the block may be invoked, using the yield statement. A block returns a value to the method that yields to it. The value of the last expressions evaluated in the block is passed back to the method as the value of the yield expression.
If the last parameter is prefixed by & (such as &action), that code block is converted to an object of class Proc. The Proc object is then assigned to the parameter. This allows you to pass a code block to a method as if it were a regular parameter.
- https://docs.ruby-lang.org/en/master/Proc.html
- https://docs.ruby-lang.org/en/master/Kernel.html#method-i-lambda
Blocks as closures
Variables in the surrounding scope that are referenced in a block remain accessible for the life of that block and the life of any Proc object created from that block. This is called a closure. More on closures: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
Enumerators
Can iterate over two collections in parallel. Enumerator class is not to be confused with the Enumerable module. The Enumerator class is used to create custom external iterator.
Variables
https://docs.ruby-lang.org/en/master/doc/syntax/assignment_rdoc.html
A variable is an identifier that is assigned to an object, and which may hold a value. A variable is not an object in Ruby, so it is a reference to an object. A local variable name may contain letters, numbers, an _ (underscore or low line) or a character with the eighth bit set.
Assignment aliases objects, potentially giving multiple variables that reference the same object. String#dup will create a new string object with the same content. String#freeze will make a string immutable. Numbers and symbols are always frozen (immutable) in Ruby.
Examples:
In a file:
Pseudo Variables
They provide information about the program’s execution environment or serve specific purposes within Ruby. Characteristics: Predefined, read-only and available throughout the program.
Pre-defined global variables
- https://docs.ruby-lang.org/en/master/globals_rdoc.html
- https://github.com/ruby/ruby/blob/HEAD/spec/ruby/language/predefined_spec.rb
- https://github.com/ruby/ruby/blob/HEAD/lib/English.rb
In irb:
Pre-defined global constants
Control Flow and Expressions
https://docs.ruby-lang.org/en/master/syntax/control_expressions_rdoc.html
Ruby has a variety of ways to control execution. All the expressions described here return a value.
Expressions and Return Values:
- Basic Operator Expressions: + - * / % **
- Command Expressions: string with backquotes or backticks will be executed as command by OS. ls.split will give array of content of the current folder. Copying , using `echo 'hi' | pbcopy` will copy the output of echo to clipboard, which is the same as system("echo '123' | pbcopy"). Copying resource attributes: `echo "#{User.first.id}" | pbcopy`.
- Assignment is setting the lvalue (left value) to refer to the rvalue (right value), and returns rvalue. Ruby has 2 forms of assignment: first an object reference to a variable or constant, ABC = 4. Second is object attribute or element reference on the left side of the assignment operator, ABC[1] = 4. Also possible, is the rightward assignment, since ruby 3.0: data => variable (e.g. 2=>x).
- For parallel assignment, to swap vales:
- Splats and assignment:
- for rvalues a,b,c,d,e = *(1..2), 3 # a=1, b=2, c=3, d=nil, e=nil
- greedy for splat for lvalue:
- Nested assignments:
Conditional Execution
- boolean expressions: Ruby has simple definition of truth: any value that is 1. not nil, or 2. the constant false, is true. So, "c", 9, 0, :a, are true, also, "", [], {} are true. The set of false values are sometimes referred to as falsey and set of true values are referred to as truthy. nil && 99 returns nil, "c" && 99 returns 99. When it’s false, the first argument is returned, when it’s truee, the second argument is returned (short circuit evaluation). There is a difference in using && and and, terms of precedence compared to the assignment. Examples: result = "" && [], which returns the #=> [], and will show result #=> [], however result = "" and [] which returns the #=> [] and will show result # => "".
- the defined? Keyword: defined? 1 #=> "expression" and defined? a #=> nil and defined? a = 1 #=> "assignment".
- Comparing objects: == equal value, ===, <=>, <, >, <=, >=, =~, eql? (equal type and value), equal? (same object id).
- if and unless:
Safe navigation operator: &., also called the lonely operator. It returns nil if the object is nil.
Loops and iterators:
https://docs.ruby-lang.org/en/master/Kernel.html#method-i-loop
Break, next:
- Use break to leave a block early.
- Use next to skip the rest of the current iteration.
Iterators:
A different way to write an each loop with a Ruby built-in looping primitive:
Block local variables:
Pattern Matching
https://docs.ruby-lang.org/en/master/doc/syntax/pattern_matching_rdoc.html
Pattern matching compares a target which can be any Ruby object to a pattern. If the target matches the pattern, the target is deconstructed into the pattern, setting the value of those variables.
Variable binding: Assign values in the target to variables in the pattern and then use those variables in the pattern.
Case pattern matching:
Methods
https://docs.ruby-lang.org/en/master/syntax/methods_rdoc.html
Defined by keyword def. You can undefine by undef.
Can begin with lowercase or underscore, followed by letters, numbers or underscores. May end with ?, !, =.
- Predicate methods end with a ? and return a boolean result.
- Bang methods end with a ! and modify the object in some way. E.g. String .reverse or .reverse!. The first one returns a modified string and the second one modifies the receiver in place.
- Assignment methods end with = and modify the object in some way.
Parentheses are optional: def hello; end is the same as def hello() end.
A method is invoked using dot syntax: receiver.method
In other words:
- We ask the object to perform an action.
- The object receives a message.
- We send a message to the object.
Preference to use parentheses in all but the simplest cases. This would be idiomatic, it means in line with the language’s conventions.
Method arguments
A class method: def self.method_name and an instance method: def method_name.
Positional arguments: are passed to the method based on their position. Keyword arguments: are passed based on the keyword and can be listed in any order. Keyword arguments: def method_name(city: "value", state:) When calling the method, you can pass the arguments in any order, but each keyword argument must be part of the call: method_name(state: "CA", city: "San Francisco").
Collect arguments into Hash with double-splat, or **: def method_name(**args). A bare single splat will catch positional arguments, bare double splat will catch keyword arguments.
Calling a method.
Method calls without parentheses are sometimes called commands.
rule: If in doubt, use parentheses.
A return statement exists from the currently executing method. It can be used to return a value from a method. If no value is specified, nil is returned.
Classes
In object oriented programming, a class is a blueprint for a domain concept.
Instances are created by a constructor. The standard constructor method is called new. When you call Bike.new, Ruby holds an uninitialized object and calls that objects initialize method, passing all arguments from .new. This sets up the object’s state. Instances have a unique object_id (object identifier).
#<Class:object_id> is the default string representation of an object.
p calls the inspect method on the object. It’s a good way to see the object’s state.
Object and attributes
Creating an accesor method is a common pattern in Ruby. Below, the def price is a getter method, which can also be rewritten to the shortcut attr_reader :price. It allows you to read the value of an instance variable. Below, def price=(price) is a setter method, shortcut (but rare) attr_writer :price. It allows you to write to the value of an instance variable. Generally, use attr_accessor :price for both reading and writing, for a given attribute (e.g. an instance variable). Below as example price_in_cents is a virtual instance variable or calculated value. An attribute is just a method that is called when you use dot syntax and is an implementation of the uniform access principle.
As summary:
- State is held in instance variables.
- External state is exposed through methods via attributes.
- Other actions your class can perform are just regular methods.
Classes working with other classes:
With the following 1.csv file:
We look at the following bike_stats.rb file:
require_relative means that the file is loaded relative to the path of the current file. ARGV is an array of command line arguments. $stderr is the standard error stream.
Specifying access control
Classes increasingly depending on other classes is called coupling. Coupling is a bad thing. It makes it hard to change one class without breaking another. Ruby gives 3 levels of access control: public methods, private methods, and protected methods (rare).
Public methods can be called by anyone, no access control is enforced. Protected methods can be called only by objects of the defining class or subclasses. Access is within the family. Private methods can not be called with an explicit receiver, it is always in the context of the current object, known as self.
preference: per method explicit access control
Reopening Classes
Monkey-patching: Process of reopening classes to add or change (utility) methods. Use with caution.
Collections
Most real programs manage collections of data. Ruby has a number of built-in classes for managing collections: arrays and hashes. Both classes have large interfaces and many methods.
Arrays
Array.new, Array.[], create a new array.
Some assignments:
Reminder: array of words = %w{one two three}, array of symbols = %i{one two three}
Hashes
Hashes known as associative arrays, maps, dictionaries, key-value stores. They are collections of key-value pairs. The index in a hash is called a key. The value or entry is the object that the key points to. Retrieve the entry by indexing the hash with the key value.
hash literals are created with curly braces, e.g. {:key => “value”, “key_2” => “value_2”} => is called hashrocket.
Override methods:
Digging
dig is a method that allows you to access nested elements of a hash. It is a safe way to access nested elements. It will return nil if any intermediate element is nil. A method on a hash, array, or struct.
Inheritance
Sharing functionality: Inheritance, Modules, and Mixins
inheritance allows you to create a class that’s a specialization of another class: e.g. subclass and superclass, child and parent.
Modules
Modules can do everything a class can do, except create instances. They are a way to group methods, classes and constants. Two benefits: 1. a namespace and prevent name clashes, 2. can be included in other classes, known as a mixin. Module names are like class names, both are global constants with an initial uppercase letter. use them with the require or require_relative method. Module constants are referenced using two colons, the scope resultion operator, e.g. Thing::SAY.
An include is a method of the Module class. The require call is at the file level, the include call is at the class level.
Example module is Kernel which is included in Object. Another is Comparable, which assumes that any class that uses it defines the method <=> (the spaceship operator).
Some Object-Oriented languages like Python support multiple inheritance (Powerful and dangerous), some like JavaScript support single inheritance (cleaner and easier to implement). Ruby is a single inheritance language, which mixins to support controlled multiple-inheritance-like capability.
Ruby provides two mechanisms for mixing in module behaviour. The first is include, which is used to add methods as instance methods to a class, and those will be looked up after the class itself is checked for a method. The second is extend, which is used to add methods directly to the receiver of extend rather than as instance methods of a class. Ruby also provides another mechanism, prepend, which is used to add methods as class methods to a class, and those will be looked up before the class itself is checked for a method. Prepend is often used for logging or other logistical information to classes.
In general, a mixin that requires its own state isn’t a mixin, it should be written as a class.
Method lookup
With multiple ways to define methods, Ruby will look for a method in the following order:
- methods specifically added to that instance using foo=Foo.new and 1. def foo.bar; end, or via 2. class << foo; def bar; end; end
- Any module added to the receiver’s class using prepend, the last module added is checked first.
- Methods defined in the receiver’s class.
- Modules added in the receiver’s class using include, the last module added is checked first.
- If not found, the same loop will happen in the receiver’s superclass.
This continues until the method is found or the end of the inheritance structure is reached. If the method is not found, Ruby will try again from the receiver’s class, now looking for method_missing, if no method_missing is found to handle the message, a NameError is thrown. Entire list of classes and modules in this lookup path can be accessed by calling the method foo.ancestors.
Super lookup
when executing a method, if Ruby encounters keyword super, it method lookup for super starts one step after the points where the method being executed is lcoated. (e.g. if in step 2, it will start at step 3). If super has no argument list, Ruby will pass the arguments that were passed to the method that called super. If super has an argument list, even an empty one, those arguments will be passed.
References:
- https://www.rafaelmontas.com/ruby-method-lookup-path/
- https://gist.github.com/robturtle/b20c5e1077ef6ab1cb73605aff0d6b1c
- https://gist.github.com/damien-roche/351bf4e7991449714533
Inheritance, Mixins, and Design
For subclassing look for is-a relationships or hierarchies. However, for has-a or uses-a relationships, use composition. Ruby on Rails makes use of inheritance, e.g. with Person inheriting from a DatabaseWrapper class (ActiveRecord). As inheritance represents an incredibly tight coupling, it should be used sparingly. It’s easy to break. Composition is more flexible, however can get messy fast.
Exceptions
Ruby uses exceptions to solve the problem of responding to errors in a program. They let you package information about an error into an object, in ruby of class Exception or in one of Exception subclasses. Documentation is https://docs.ruby-lang.org/en/master/Exception.html. Most important subclass is StandardError, which along with its subclasses, should be used to capture all errors in code. The other subclasses are used to indicate specific types of errors, e.g. Ruby internals or system-level problems.
Every Exception object has:
- The type (the exception’s class): StandardError, RuntimeError, etc.
- Optional message: “This is the message”
- Optional backtrace: An array of strings, e.g. [“file:line”, “file:line”, …] https://docs.ruby-lang.org/en/master/Exception.html#method-i-backtrace
How to raise an exception:
Handling exceptions
We enclose the code that could raise an exception in a begin/end block and use one or more rescue clauses to handle the types of exceptions.
Below is an example of a begin/rescue/end block. We catch all exceptions related to StandardError and its children, and re-raise them. The global variable $! contains the exception object. The warn method is used to print to standard error. The raise method is used to re-raise the exception. The exclamation point ! presumably is used to indicate our surprise that our code failed. You can have multiple rescue clauses, and the first one that matches will be executed. The rescue clause can have a variable name, which will be assigned the exception object, usually named e, like: rescue StandardError => e. If you write rescue without a parameter, it will default to catch all StandardError exceptions. If you need to guarantee that a certain processing is done at the end of a block of code, with or without an exception being raised, use ensure. The else clause is only executed if no exceptions are raised.
If no rescue clause matches or if an exception is raised outside of a begin/end block, Ruby moves up the stack, looking for an exception handler in the caller, and so on until it finds one. If no exception handler is found, the program typically halts.
Sometimes you want to use the retry clause. This will repeat the entire begin/end block, so can create infinitie loops and therefor best used with a counter.
Raising exceptions
You can raise exception with raise or fail.
raise simple reraises the exception. raise with a string argument will create a new RuntimeError exception. raise with a class name will create a new exception of that class, with the second argument as the message. raise with a class name, a message, and a Kernel#caller stack trace, will allow to edit the stack backtrace as well: raise InterfaceError, "keyboard failed", caller[0..-2].
You can also define your own exceptions by subclassing Exception or one of its subclasses, to hold more information about the error, or possibly add additional behavior.
Using Catch and Throw
catch defines a block that is labeled with given name (Symbol or String) and is normally executed until a throw is encountered. When throw is encountered, the block is exited and returns nil or, when second parameter is passed, that value is returned.
Exception resources:
Input and output
https://docs.ruby-lang.org/en/master/IO.html
I/O or IO methods are implemented in the Kernel module, including gets, open, print, printf, putc, puts ,readline, readlines, and test. These are available to all objects. There is also Ruby’s IO class, with subclasses File and BasicSocket with more specialized methods. The IO object is a bidirectional stream between a Ruby program and some external resource.
Open and Close files
Read and Write files
Kernel#gets reads a line from the standard input (or from a specified file), File#gets reads a line from a file.
Reading from console:
Reading from a file, line by line:
From a file with IO#each_line with String#dump to show the line:
Giving each_line an argument will split the line on that argument:
Iterator with autoclosing block feature:
Reading a file into a string:
Reading a file into an array:
Writing to a file:
Every object you pass to puts is converted to a string with to_s method. Note: puts adds newline after the output, print does not.
Find files
StringIO behaves like other IO objects, but they read and write strings, not files. https://docs.ruby-lang.org/en/master/StringIO.html
Talking to Networks
At the network level, Ruby comes with a set of classes in the the socket library. https://docs.ruby-lang.org/en/master/Socket.html These give access to TCP, UDP, SOCKS, and Unix domain sockets, and additional socket types. At a higher level of the OSI model, the “lib/net” here https://docs.ruby-lang.org/en/master/Net.html and https://github.com/ruby/ruby/tree/master/lib/net, provides application level protocols (such as HTTP, HTTPS, FTP, POP, IMAP, and SMTP). Net::HTTP for example: https://docs.ruby-lang.org/en/master/Net/HTTP.html or at a higher-level the open-uri library is a wrapper for Net::HTTP, Net::HTTPS and Net::FTP, and handles redirects automatically: https://docs.ruby-lang.org/en/master/OpenURI.html
IO is however slow and blocks programs, a common workaround is to use threading to do multiple things at once.
Concurrency
When writing programs that are doing multiple things at once, each thing is called a thread. And the goal is to have thread safety, meaning the code will execute correctly no matter what order the threads operate. When the order of operations matters, we call it a race condition, and it’s bad because it can lead to hard-to-find bugs. Ruby programs have a Global Interpreter Lock (GIL), which means that only one thread can executed by Ruby at any time. This is one way to protect thread safety and prevent race conditions.
The Thread class is the basic unit of multithreaded behavior in Ruby. Ruby also allows you to spawn processes out to the underlying operating system, and mulithread those processes. Fibers are an additional abstraction, to suspend the executation of one part of a program and run some other part. The Ractor library allows you to bypass the GIL and have ‘true’ multiple threading using Ruby.
Threads
https://docs.ruby-lang.org/en/master/Thread.html
The lowest-level mechanism is the Thread class. Mostly you will see one thread executing, and another waiting on an I/O operation. A thread shares all global, instance, and local variables that are in existence and available at the time the thread starts. Threads are immediately executed. Local variables created in the thread’s block are truly local ot that thread. Thread.join will ensure the main program waits for the threads to finish, you can also give the thread a timeout, and it will return nil if the thread does not finish in time. Normally, building timing dependencies in a multithreaded program is a bad idea. However, if you need to do this, you can use the Mutex (mutually exclusive) class, which creates areas of code that can only be accessed by one thread at a time. Some of the relevant methods to enable this: Mutex#lock, Mutex#unlock, and the block version Mutex#synchronize, and the Mutex#try_lock method.
Multiple external processes
Kernel#system executes given commmand in a subprocess and returns true if the command was found and executed properly. If it fails, it returns false and the subprocess’s exit code is in $?.
When we want to have more control, we can use the IO.popen method, which returns an IO object.
Sometimes we can run a subprocess independently:
Fibers
https://docs.ruby-lang.org/en/master/Fiber.html
Fibers are a block of code that can be stopped and restarted, which is sometimes called a coroutine. They are cooperatively multitasked, meaning that the responsibility of control is with the fibers and not the OS. Fibers are not immediately executed. When resume is called, the fiber will execute until it hits a yield statement, which suspends execution. The last expression evaluted will be the return value of the Fiber.
Ractors
https://docs.ruby-lang.org/en/master/Ractor.html
Ractors are a way to bypass the GIL and have ‘true’ multiple threading using Ruby. Ractor is a chunk of code that has a single input port and a single output port. So like a physical room, with a single entrance and a single exit door. The entrance door could have a queue to get in. Ractor is created via Ractor.new and is isolated, the code inside the block won’t be able to acces any variables that aren’t defined in the block (no globals and no external locals).
Testing
Why?
- To ensure maintenance and ongoing changes are not breaking existing code.
The options for testing are:
No framework, just Ruby:
Minitest
Gives you three facilities wrapped into a package:
- a way of expressing individual tests
- framework for organizing tests
- flexible ways of invoking tests
Instead of if or unless, we write assertions.
The minitest/autorun module includes minitest itself, which has most of the features we’ve talked about and call Minitest.autorun, which calls the test runner. Test files are being executed as plain Ruby files. Unit tests are organized into higher-level groupings, called test cases (around of facility or feature). Test cases are organized into test suites. Classes that represent test cases must be subclasses of Minitest::Test. Methods that begin with test_ are test methods. We can use shared code in setup and teardown methods, which for setup will run before each and every test method, or with teardown, after each test method finishes.
The idea of unit tests is fast-running, context-independent, and easy to maintain.
Minitest allows you to create mock objects, which simulate the API of an (existing) object, providing a canned response instead of a more expensive or context-dependent real response. Mock objects can be verified to ensure that they were called with the correct parameters. We test the behavior of the object, not the return value.
Minitest mock objects can take an optional third argument, which is an array of arguments, and an optional block argument. If those arguments are used, then the mock object only accepts the method calls that match the arguments and block. If not, it raises an MockExpectationError. More documentation in the minitest mock class
It’s common to want to orride one method on an existing object rather than create an entire mock object. In minitest, you can do this with the stub method, which is added to Object, so it’s available to all objects. More documentation in the minitest Object class extension with stub
The first argument to stub is the name of the method you want to intercept, as a symbol. The second argument is the value that should be returned, or you can pass a block argument. The return value of the stub is one of these:
- value returned of the the block
- result of the second_arg.call, if second_arg responds to call, meaning it’s usually a Proc or lambda
- the second argument itself, if neither of the above
To have control over the depth of runnin tests, be able to run tests with:
- an exact match: test test_file_name.rb -n exact_match
- a pattern: test test_file_name.rb -n /all_with_pattern/
- a single file: test test_file_name.rb
- a group of files into a test suite; create a file with a name for a test suite and require the test files: test test_suite_name.rb
RSpec
Some history: https://stevenrbaker.com/tech/history-of-rspec.html
RSpec started as a teaching tool, but it was so popular that it became a real tool. The goal of RSpec is to express thinking as close to natural language. RSpec is concerned with driving the design, as such, words like expectation and specifcation are used, and usually RSpec is used before you write the implementation. A ‘spec’ is a specification, a description of how something should work, and written before implementation, an ‘assertion’ is used to test what already exists.
To be clear, you can use both RSpec after you write code, and Minitest (also with specs) before you write code.
An example specification file:
- that describes how a Roman class should behave
- that groups based on ‘converting arabic numerals to roman numerals’:
- the group shows 4 expectations that start with ‘it’
It is helpful to see With parenthesis and implicit self message receivers (self.describe, self.it, self.expect, and self.eq):
The expect(something).to eq(something_else) is the most common way to write an expectation in RSpec. The result of a call to eq is a matcher. More in Github rspec expectations: https://github.com/rspec/rspec-expectations?tab=readme-ov-file#built-in-matchers
Using a before method is first step to prevent duplication in setup of tests.
However, RSpec gives the let method as alternative and preferred way to setup tests. The let block is only evaluated when the variable is used, and the block is evaluated once, and further uses use the value of the first evaluation.
In RSpec, the term for a fake object is test double, the object that stands in for the real object (stunt double) https://github.com/rspec/rspec-mocks?tab=readme-ov-file#test-doubles. You can create a double and assign it a method to respond to, and a value to return, with allow. You can limit the arguments to the method with with. You can also define multiple methods with receive_messages. In Minitest we validated a mock being called, in RSpec we manage this by using expect. Expect behaves the same as allow, however RSpec automatically verifies that the method was called, if not it fails the spec. However this is implicit and at the end, so might be harder to find. You can also use allow and expect as stub on object that are not test-doubles.
A short overview:
Oneliners
To make your file executable, the mode of the file is metadata that determines if the current user can read, write, or execute the file.
After having made the file executable, we need to tell the system that the file should be run through the ruby interpreter. This is done by adding the following line as the first line of the file:
To run the file, you can use:
If you want to run the file with options for Ruby itself, you can use for example, to show warnings:
Options after the file name will be processed as arguments to the script.
Passing arguments to the script
ARGV (argument vector) is an array of arguments passed to the script. All values in ARGV are strings.
ARGF is a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN. It assumes all arguments are files.
https://docs.ruby-lang.org/en/master/ARGF.html
This will read the files and print the lines in reverse order. The reverse.rb script will be:
To invoke the script with -i.bak option:
This will generate a backup file with the .bak extension. The other files will be modified in place.
Option parsing
https://docs.ruby-lang.org/en/master/OptionParser.html
In the example.rb file, we can parse options with the OptionParser class:
The output of the script will be:
The options hash will contain the options that were passed to the script. The parse! method will walk through ARGV array, calling the blocks for any options that it finds and deleting the options and their arguments from ARGV. Also check Thor, which is a library for creating command line tools: https://github.com/rails/thor/wiki.
Environment variables
Operating system environment variables are available via ENV in the script or irb.
Where Ruby finds its libraries
Use require to load libraries into the program.
Where Ruby is holding libraries can be shown with.
$: is an array of paths where Ruby is holding libraries.
Rake build tool
With Rake you can define tasks and task dependencies, and run them from the command line.
For example, editing files, and creating backups. Backups on Unix systems usually have a tilde at the end of the file name, for windows files usually have a .bak extension.
Example ruby program:
With Rake, we create a Rakefile to define the tasks.
This can be executed with rake delete_unix_backup_files. We can compose tasks with dependencies, for example, rake remove_all_backup_files which will first run delete_unix_backup_files and then delete_windows_backup_files. If a Rake taskis named default it will be executed if no other task is specified.
Ruby Gems
https://guides.rubygems.org/what-is-a-gem/
A gem is a library of Ruby code that can be installed and used in a Ruby program. RubyGems is the command-line tool for managing gems, it comes as gem command and is standard in Ruby. Bundler is a tool for creating manifests of gem versions, so that develoeprs use the same version of gems in their projects.
full description of all the gem command-line options: https://guides.rubygems.org/command-reference/
Some examples:
When we install a gem, it interacts with the current Ruby runtime to place the gem in a known location.
After installing a gem, we can use it in our program:
You can also browse the source code of a gem with gem open GEM_NAME.
Using Bundler
Bundler is a ruby gem that manages dependencies for a project. It is used to install gems and their dependencies, and to lock the version of the gems in the project. To do this, we usually need to create a Gemfile in the root of the project. The Gemfile is actually Ruby code, each line is a Ruby method call and syntax is the same as in a regular Ruby program. To install the gems, we need to run bundle install which will create a Gemfile.lock file to store the actual version of the gems that were installed. Using Bundler, we can also run the tests with bundle exec rspec, which will use the gems installed in the Gemfile.lock file. Using require 'bundler/setup' in the program will load the gems from the Gemfile.lock file, ruby on rails uses this to load the gems.
Instead of using bundle exec rspec, we can use binstubs. Binstubs are scripts that are created in the bin directory of the project. Example: bundle binstubs rspec-core will create a bin/rspec script that will run the rspec-core gem.
Example of ./bin/rspec script, so bundle exec rspec is the same as running bin/rspec. You can further shorten to rspec by adding bin/rspec to your PATH with export PATH="./bin:$PATH".
Sometimes we don’t want to auto require the gem:
The command for upgrading gems is bundle update. Finally, we can use gemfile groups to group gems into different categories, for example:
Besides a Gemfile, we can also use require bundler/inline to load the Gemfile from the current directory. This is useful for single file scripts.
Writing and packaging your own code into a gem
There are 2 questions to organization of code:
- How to prevetn different things with the same name to clash?
- How to organize source files in the project?
It’s smart to split code into a trivial driver file, that provides the external interface (command-line interface), and one or more files with the functionality. Your tests will be able to test the functionality without running the whole program via the driver file.
Also, to structure the code and to answer the first question, we can use a Class to encapsulate the functionality. This will ensure the code is properly scoped to a namespace. To access the contants you can use the namespace resultion operator :: (a double colon).
Default setup for a gem:
This will empower each Ruby user to use it with gem install my_gem.
Interactive Ruby (irb)
irb is a command-line ‘shell’ that allows you to interact with Ruby code in a REPL (Read-Evaluate-Print Loop) environment.
The irb command starts the interactive Ruby shell. It stores result in the _ variable.
We can create a file, which we can load into irb, either with irb -r or with load method.
Example:
You can potentially use multiple irb sessions at the same time, so during a session you can type irb and then you’ll get a new session. The jobs command will show all irb sessions, and you can switch with fg 0 (0 is the session number).
When you specifc an object when you create a subsession, that object becomes the value of self in that binding. So, if you create a subsession with irb -r my_file.rb and then type self, it will be the object that was passed to the subsession.
Configuring IRB
When you start IRB, which loads from .irbrc file in your home directory, it will load libraries, define methods, and set up the environment based on the content.
IRB resources:
- https://github.com/ruby/irb
- https://github.com/janlelis/irbtools
- https://github.com/awesome-print/awesome_print
- https://github.com/ruby/irb/blob/master/lib/irb/completion.rb
- From the same author:
- https://gist.github.com/search?l=Ruby&o=desc&p=1&q=irbrc&s=updated
Ruby and the Web
Web utilities
The Common Gateway Interface, CGI, can handle characters, especially for HTML encoding/escaping, URL encoding, and form data parsing. Also it can handle cookies, which are a way of letting web apps store their state on the user’s machine. https://github.com/ruby/cgi
Templating with embedded Ruby (ERB)
We can embed Ruby in an HTML document via ERB, which is a standard Ruby library and acts as a command-line filter. e.g. you can do erb -x file_name.rb whereby -x Displays the resulting Ruby script. https://github.com/ruby/erb
Serving Ruby to the Web
Rack is the interface for the relationship between a web server and web application https://github.com/rack/rack. A user request comes as text, whose structure is defined by the HTTP specification. The web server’s job is to convert that text into Ruby objects and pass them to the framework. The framework’s job is to take that object, do something with it, and return Ruby objects to the web server, which converts them back to HTTP text or data to send to the browser.
So Rack provides:
- a standard structure for the user request: a hash structure with pre-defined set of keys
- a standard structure for the response: an Array with 3 elements; returns status, headers and the body
- a mechanism for the interface between the two; A rack app is a method or block, that takes the environment as input and returns the repsonse as output.
Ruby in the browser with Web Assembly
WASM https://developer.mozilla.org/en-US/docs/WebAssembly allows to run Ruby in the browser https://github.com/ruby/ruby.wasm.
Ruby Style
There are two distinct kinds of Ruby style. The first is syntax-based, and can be evaluated by a linting program, e.g. RuboCop and Standard. The second is the one based around how to use Ruby as a tool, e.g. Ruby’s dynamism and support of dynamic typing (“duck typing”) to make intent clearer, less code to maintain and change easier. Avoid “bikeshedding” - spending time on trivial cosmetic issues like what color to paint the bike shed. Most common style guide comes from the rubocop linter: https://github.com/rubocop/ruby-style-guide. More about linters: https://en.wikipedia.org/wiki/Lint_(software).
In general, for readability: No spaces after (, [ or before ], ). Use spaces around { and before }.
Single-line method calls should only be used when the body is a single expression and has no side effects.
To use rubocop and see offenses:
- installing: gem install rubocop
- running: rubocop. Each file will get as output one character. A dot means no issues, I is for info, R for refactor, C for convention, W is warning (still legal Ruby), E is for error (not legal Ruby), F is for fatal (certainly not legal Ruby), meaning the file has syntax error that prevents it from being parsed.
To get an overview use rubocop --format offenses.
Style Resources
- cookpad ruby styleguide
- cookpad global style guides: ruby
- cookpad global style guides: rails
- rubocop ruby styleguide
- shopify ruby styleguide
- thoughtbot ruby styleguide
- thoughtbot rails styleguide
- rubocop rails styleguide
- https://github.com/leahneukirchen/styleguide/blob/master/RUBY-STYLE
Typed Ruby
Ruby does not have basic types. A variable is an instance of a class. It can be useful to know what type of values can be assigned to a variable. Setting the type of a variable, attribute, or method argument limits the set of values that can be assigned to it. In Ruby, x / y is equal to the method call: x./(y) .
When you have to declare the type of a variable before it is used, it’s explicit typing. Some languages infer the type of a variable from the first usage, e.g. x = 3 in TypeScript, this is called type inference.
Usually a tool, often part of the compiler, that evaluates every variable interaciton to see if type information is followed. If you later in TypeScript set x = "foo", there will be a compliation error, as foor is string. This is called static typing. Ruby determines if a variable can receive a method only at runtime, this is called dynamic typing. The process of determining the behavior of the method at the last possible moment is called late binding.
3+ “3”, which will coerce the string to integer and add both, is called weak typing, 3+”3” which fails is called strong typing.
RBS
The standard Ruby typing system is called RBS (Ruby Signature) https://github.com/ruby/rbs. You create a separate file that contains type signature information for all or part of your code.
In the file you can describe what type of data Ruby can expect and return.
RBS Types:
RBS examples:
RBS Resources:
- https://github.com/ruby/rbs
- https://github.com/ruby/rbs/blob/master/docs/syntax.md
- https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md
- Usage can be seen at https://github.com/ruby/rbs/network/dependents?dependent_type=PACKAGE
Sorbet
Besides RBS there is the third-party tool by Stripe called Sorbet https://github.com/sorbet/sorbet. It is different from RBS as the type annotations go in the Ruby file. Also, Sorbet can do static analysis and type checking at runtime (for development experience a must). RBS can only do static analysis and runtime type checking during tests.
General docs: https://sorbet.org/docs Quick reference https://sorbet.org/docs/quickref The playground which has examples located in the top right: https://sorbet.run/.
Sorbet type examples
Besides the syntax, we can have general rules to describe common sense.
General rules
- Line: limit lines to 80 characters.
- Parameters: no more than 4 parameters into a method. Hash options are parameters.
- Methods: can be no longer than 5 lines of code.
- Classes: can be no longer than 100 lines of code.
- Controllers: can instantiate only 1 object. Therefore, views can only know about one instance variable and views should only send messages to that object (@object.collaborator.value is not allowed).
Organizing a Model:
RuboCop details an ExpectedOrder of Class Structure https://github.com/rubocop/rubocop/blob/master/lib/rubocop/cop/layout/class_structure.rb
References
- https://zenspider.com/ruby/quickref.html
- hopsoft rails standards rails-4-X
- https://thoughtbot.com/blog/sandi-metz-rules-for-developers#only-instantiate-one-object-in-the-controller
- controllers should only use CRUD actions
Share via