module Kernel

Practical examples and pitfalls for Kernel

Kernel examples: format output and leave a nested search

Practical notes by Ruby-Doc.org

Format the display after calculating the value

format is one of the methods available through Kernel. Here it produces a small report line with a named label and count. The placeholders show which value fills each position, without making the calculation itself deal with padding.

Example 1
line = format("%<name>-8s %<count>03d", name: "box", count: 7)
p line
Expected output
"box      007"

The widths are minimums. A longer name will not be truncated to eight characters, so a report with a strict visual limit needs a separate rule for long labels. Keep the count as a number until text is required; formatting should not make later arithmetic depend on parsing a display string.

Return the first match from a nested search

This search needs to leave both loops as soon as it finds an even number. A throw returns the result through the matching catch. The fresh object used as the tag belongs only to this search.

Example 2
rows = [[3, 5], [7, 12], [14, 16]]
tag = Object.new
match = catch(tag) do
  rows.each_with_index do |row, index|
    row.each { |number| throw(tag, [index, number]) if number.even? }
  end
  nil
end
p match
Expected output
[1, 12]

If no value matches, execution reaches the trailing nil, which becomes the result. A break in the inner loop would leave only that loop. A helper method or a conventional iterator may be easier to read in other searches; use this form where leaving the nested traversal explains the intent clearly.

Despite its name, throw is not an exception for rescue to handle. It pairs with catch as a control-flow operation. Confusing those mechanisms can make an error handler appear present while it has nothing to do with the operation being performed.

Return useful data from a reusable helper

Printing is convenient during exploration, but a reusable calculation generally gives its caller more choice by returning its result. The caller can print, store or test that result. Unexpected output from a library helper is harder to redirect or compose.

Check return values before chaining unfamiliar convenience methods. A method whose purpose is a side effect may not return the data that the next call expects. A short, controlled example is often enough to make that contract visible.

API reference: Kernel API reference

Related: Enumerable · Exception

Put the following line in your login script (e.g. ~/.bash_profile) with modified path:

export RUBYOPT="-r /path/to/debug/prelude ${RUBYOPT}"

trap

Kernel extensions for minitest

The Kernel module is included by class Object, so its methods are available in every Ruby object.

The Kernel instance methods are documented in class Object while the module methods are documented here. These methods are called without a receiver and thus can be called in functional form:

sprintf "%.1f", 1.234 #=> "1.2"

What’s Here

Module Kernel provides methods that are useful for:

Converting

Querying

Exiting

Exceptions

IO

Procs

Tracing

Subprocesses

Loading

Yielding

Random Values

Other