Ruby quick reference
Ruby 4.0 essentials: values and types, control flow, methods and blocks, arrays and hashes, classes and modules, strings, input and files — and how a Ruby program runs in Zerethon IDE.
Written for Ruby 4.0. Examples that show an output were run in the Zerethon sandbox (Ruby 4.0) on 2026-10-24.
Basics
-
Hello, World
The smallest Ruby program, and the difference between
puts,printandp. -
Variables and types
Numbers, strings, symbols,
true/false,nil, and converting withto_i,to_f,to_sandInteger(). -
Conditions and loops
if/elsif/unless,case, iterating witheachandtimes,while, andnext/break. -
Methods and blocks
Define methods with
def, keyword arguments, implicit return, blocks withyield, and lambdas.
Data
-
Arrays and hashes
Arrays and hashes,
map/select/sort_by,fetchwith a default, and other everyday Enumerable methods. -
Classes and modules
Define classes with
initializeandattr_reader, inherit with<, mix in modules, and useStruct. -
Strings
Interpolation, common string methods,
format, and regular expressions withscan,=~and[].
Input and files
-
Reading input (stdin)
Read what you type in the IDE's Input tab with
getsor$stdin.each_line. -
Reading and writing files
Read project files with
File.readandCSV, write JSON, and keep temporary files under/tmp. -
Several files and libraries
Load your own files with
require_relative, and what Ruby offers without installing gems.
In Zerethon IDE
-
Running Ruby in Zerethon IDE
What Run does with your project, the time limit, input, tests, and what a run cannot do.
-
Common errors
What
NoMethodErroronnil,NameError,ArgumentError,TypeError,ZeroDivisionErrorandSyntaxErrormean and how to fix them. -
Official documentation
Where to read more: the Ruby documentation site, the quick start, and the core and standard library reference.