begin end

suggest change

The begin block is a control structure that groups together multiple statements.

begin
  a = 7
  b = 6
  a * b
end

A begin block will return the value of the last statement in the block. The following example will return 3.

begin
  1
  2
  3
end

The begin block is useful for conditional assignment using the ||= operator where multiple statements may be required to return a result.

circumference ||=
  begin
    radius = 7
    tau = Math::PI * 2
    tau * radius
  end

It can also be combined with other structures such as rescue, ensure, while, if, and unless to control program flow.

begin blocks are not closures, like { ... } or do ... end. They cannot be passed to methods.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents