Length 19 (by a guest)
[1, 2].each{|i|p i}
Ruby supports for
loops, but the .each
method is preferred. The code in brackets is a block, essentially a single-use anonymous function. Blocks can take parameters, which are enclosed in pipe characters. The p
is a shortcut for puts [something].inspect
. The .inspect
method is a variation of .to_s
which provides more details about the object.
Length 18