Part 0 · 1 chapters · ~8 min

Basics

Installing Ruby with a version manager, everything as an object, numbers, strings and symbols, arrays and hashes, blocks, procs and lambdas, control flow, methods with keyword arguments, classes and modules, Enumerable, and Bundler and gems.

1

Ruby in one screen

code
# install a current Ruby (3.3+) with a version manager; the macOS system Ruby is old
mise use [email protected]      # or rbenv / asdf / chruby

def fee(amount_kobo, rate_bps: 50, minimum: 1_000)            # keyword arguments with defaults
  raise ArgumentError, "amount must be positive, got #{amount_kobo}" unless amount_kobo.positive?
  [amount_kobo * rate_bps / 10_000, minimum].max
end

fees = [100_000, 5_000_000].map { |a| fee(a) }                # blocks: => [1000, 25000]
by_owner = transfers.group_by(&:owner_id)                     # &:sym turns a method name into a block
big, small = transfers.partition { |t| t.amount_kobo > 10_000_000 }
total = transfers.sum(&:amount_kobo)

5.times { |i| puts i }            # everything is an object, even integers
:pending.to_s                     # symbols: immutable, interned names (hash keys, states)
{ currency: "NGN", amount: 500 }  # hash with symbol keys

Money in Ruby: Integer minor units, or BigDecimal (never Float); the money gem adds currency-aware objects. Ruby Integers grow arbitrarily large, so overflow is not a concern.