Ruby
Ruby is a dynamic, pure object-oriented language favored for Rails, scripting, and developer happiness.
Reserved Words
- __FILE__, __LINE__, BEGIN, END
- alias, and, begin, break
- case, class, def, defined?
- do, else, elsif, end, ensure
- false, for, if, in
- module, next, nil, not
- or, redo, rescue, retry
- return, self, super, then
- true, undef, unless, until
- when, while, yield
Core Object Model
- Everything is an object, even classes and integers.
- Open classes allow runtime extension (`class String; end`).
- Mixins with modules share behavior without inheritance.
- Symbols (e.g., :status) are immutable interned labels.
- Blocks, Procs, and Lambdas encapsulate behavior for iteration.
Syntax Highlights
- String interpolation: "Hello #{name}".
- Ranges: (1..10) inclusive, (1...10) exclusive.
- Keyword arguments with defaults: `def create(user:, admin: false)`.
- Safe navigation: `user&.profile&.city`.
- Pipelines via Enumerator::Lazy and pattern matching (Ruby 3+).
Essential Classes & Methods
- Enumerable: map, select, reject, reduce, any?, all?, chunk, group_by.
- Array: push/pop, shift/unshift, sample, zip, transpose, combination.
- Hash: fetch, dig, transform_keys, symbolize_keys (ActiveSupport).
- String: gsub, sub, split, strip, chars, each_line.
- Kernel: puts, print, p, loop, require, raise.
- File: open, read, write, each_line, binmode.
Operators
- Arithmetic: + - * / % **
- Comparison: == != < > <= >= <=>
- Logical: && || ! not and or
- Assignment: = += -= *= /= %= **= ||= &&=
- Range: .. ...
- Defined? and ternary: condition ? a : b
Frameworks & Tooling
- Ruby on Rails for full-stack web apps.
- Hanami, Sinatra, and Roda for lighter services.
- Bundler manages gems (`bundle init`, `bundle install`).
- Rake provides task automation, similar to makefiles.
- RSpec, Minitest, and Cucumber for testing.
Best Practices
- Favor symbols for keys, freeze constants, and leverage immutable data.
- Use RuboCop for linting and formatting.
- Encapsulate metaprogramming behind clear APIs.
- Prefer enumerators and lazy evaluation for large datasets.
- Adopt the "Convention over Configuration" mindset when using Rails.