module Kernel

The program's functions (puts, gets, rand, exit, ...), included in Object: private, so they are called without a receiver, as in Ruby.

<prelude>:6734

Methods

def puts(*args: Object) -> Void

Writes each argument's to_s to standard output, followed by a newline unless it ends with one. An array writes its elements, each on its own line (an empty one, an empty line); nil and no argument write an empty line.

<prelude>:6742

def print(*args: Object) -> Void

Writes each argument's to_s to standard output, with no separator and no newline.

<prelude>:6745

def p[T](value: T) -> T

Writes value's inspect and a newline to standard output, and returns value, so it can wrap an expression.

p(1 + 2)   # => 3

<prelude>:6752

def p(*values: Object) -> Void

Writes each value's inspect on a line of its own; returns nil.

<prelude>:6754

def pp[T](value: T) -> T

The same as p with one value.

<prelude>:6756

def warn(*args: Object) -> Void

Writes the arguments to standard error as puts writes them to standard output (flushing standard output first).

<prelude>:6759

def loop(&: () -> Void) -> Void

Runs the block again and again, until a break (or a return or an exception) leaves it. A StopIteration raised in the block ends the loop, so loop { e.next } stops at the end of an enumerator. As no loop does, it neither lets other tasks run nor checks for a cancellation by itself: one that only computes calls Task.yield.

n = 0
loop do
  n += 1
  break if n == 3
end
n   # => 3

<prelude>:6774

def sleep(duration: Duration) -> Void

Suspends the current task for duration (Task.sleep): other tasks run in the meantime.

<prelude>:6777

def exit(status: Int = …) -> Nothing

Ends the program with status, as Ruby's: raises SystemExit, so the ensure clauses it leaves run, innermost first, then the at_exit blocks. A plain rescue lets it through; rescue SystemExit stops it.

In another task, the SystemExit ends the task as any exception does: await, the task's Task.scope, Task.timeout and Task.blocking raise it again in their caller. It is also raised where it next waits in the main task (once, when it is the one waiting for the task), as Ruby raises a thread's in the main thread: an exit in an HTTP handler ends the program. A Task.timeout block is its caller's code: while it runs, the exit of a task spawned in it, or one meant for the task waiting in it, is raised in the block instead (so quarz test catches one in a test, parallel or not).

<prelude>:6791

def exit!(status: Int = …) -> Nothing

Ends the program with status at once: no ensure clause and no at_exit block runs. What the task's thread wrote is flushed; as with Ruby's exit!, output other threads still hold may be lost.

<prelude>:6795

def abort(message: String) -> Nothing

Writes message to standard error and raises SystemExit with status 1 and message, as exit does.

<prelude>:6798

def at_exit(&blk: () -> Void) -> Void

Runs the block when the program exits.

<prelude>:6800

def gets -> String

The next line of standard input, with its newline (the last line may have none).

Raises

EOFError at the end of the input. Unlike Ruby, gets does not return nil there: gets? does.

<prelude>:6808

def gets? -> String?

The next line of standard input, with its newline, or nil at the end of the input.

<prelude>:6811

def readline -> String

The same as gets: the next line, raising EOFError at the end.

<prelude>:6813

def readlines -> Array[String]

The remaining lines of standard input, each with its newline; empty at the end of the input.

<prelude>:6816

def printf(format: String, *args: Object) -> Void

Writes format(format, *args) to standard output.

<prelude>:6818

def rand -> Float

A random Float in 0.0...1.0, from the default generator (seeded by srand; Random makes generators of their own).

<prelude>:6821

def rand(max: Int) -> Int

A random Int in 0...max.

Raises

ArgumentError when max is zero or negative (Ruby's rand(0) returns a Float).

<prelude>:6828

def rand(max: Float) -> Float

A random Float in 0.0...max.

Raises

ArgumentError when max is zero or negative.

<prelude>:6834

def rand(range: Range[Int]) -> Int

A random element of range.

Raises

ArgumentError for an empty range.

<prelude>:6840

def rand(range: Range[Float]) -> Float

A random Float in range, from its start up to, not including, its end (.. or ...). A range whose ends are equal, even an empty one (5.0...5.0), gives its start.

Raises

ArgumentError when the end is below the start.

<prelude>:6848

def srand(seed: Int) -> Int

Seeds the default generator; returns the previous seed.

The same seed gives the same sequence of rand results.

<prelude>:6852

def srand -> Int

Seeds the default generator with a fresh random seed; returns the previous seed.

<prelude>:6855

def format(format: String, *args: Object) -> String

The arguments formatted by format, a subset of Ruby's sprintf: %d %i %s %f %e %g %x %X %o %b %c %%, with the flags -, 0, + and space, a width and a precision. %s takes any value's to_s. A conversion without a value formats nil. %e writes the exponent without a + or padding (1.500000e3, where Ruby writes 1.500000e+03).

Raises

ArgumentError for a conversion it does not know (%p).

format("%05.1f|%-3d|%x", 3.14159, 7, 255)   # => "003.1|7  |ff"

<prelude>:6870

def sprintf(format: String, *args: Object) -> String

The arguments formatted by format, a subset of Ruby's sprintf: %d %i %s %f %e %g %x %X %o %b %c %%, with the flags -, 0, + and space, a width and a precision. %s takes any value's to_s. A conversion without a value formats nil. %e writes the exponent without a + or padding (1.500000e3, where Ruby writes 1.500000e+03).

Raises

ArgumentError for a conversion it does not know (%p).

format("%05.1f|%-3d|%x", 3.14159, 7, 255)   # => "003.1|7  |ff"

<prelude>:6872

def Integer(text: String) -> Int

text as an Int, as Ruby reads it: an optional sign, then the base from a prefix (0x hexadecimal, 0b binary, 0o octal, 0d decimal, either case), octal for a bare leading 0 as in a literal (017 is 15), decimal otherwise. Unlike String#to_i, the whole text must be the number: whitespace may surround it, and a single _ stand between digits. For a zero-padded decimal ("08"), pass base 10.

Integer("0x1f")       # => 31
Integer(" 1_000 ")    # => 1000
Integer("017")        # => 15
Integer("017", 10)    # => 17
Integer("08")         # raises ArgumentError
Integer("12abc")      # raises ArgumentError

Raises

ArgumentError for a text that is not an integer, OverflowError for one too large for an Int.

<prelude>:6893

def Integer(text: String, base: Int) -> Int

text as an Int in base (2 to 36, or 0 for the one-argument form's reading), read as that form reads it: the prefix naming base is allowed (Integer("0xff", 16) is 255), a leading 0 is not octal.

Raises

ArgumentError for a text that is not an integer in that base, or a base out of range; OverflowError for one too large for an Int.

<prelude>:6902

def Float(text: String) -> Float

text as a Float: a decimal or exponent literal, with surrounding whitespace and _ between digits allowed.

Raises

ArgumentError for a text that is not a number.

<prelude>:6909

def String(value: Object) -> String

value.to_s.

<prelude>:6911