Blocks, modules, metaprogramming basics and idiomatic Ruby.
A vibration sensor reports a list of readings. Write an iterator
method each_window(readings, size) that yields every run of size
consecutive readings (as an Array) in order. When it is called
without a block it must return an Enumerator over the same
Write a helper with_retries(max_attempts) that runs the given block,
yielding the current attempt number (starting at 1). If the block
raises a StandardError and attempts remain, run it again; otherwise
let the exception propagate. On success return the block's value.
Customer names arrive messy from several import sources. Each source
is configured with a list of cleanup step names. Write
normalize(value, steps) that applies the steps in order and returns
the result. Keep the steps as lambdas in a hash and apply them one
Write make_limiter(limit) that returns a lambda taking a key (for
example an API client id). Each call counts one request for that key
and returns true while the key has made at most limit requests,
false afterwards. The counts must live inside the closure: no
A teammate writes a helper that should run some work inside a database transaction and log its duration:
In a code review you see invoices.map(&:total). Explain what the &
does at a call site, how &:total ends up calling total on each
invoice, and a case where &:sym doesn't fit and you should write the
block out.