0

7.2 Blocks in Ruby says..

"block is a piece of code that can't be stored in a variable and isn't an object. It is, as a consequence, significantly faster than a lambda, but not as versatile and also one of the rare instances where Ruby's 'everything is an object' rule is broken"

Going by Best explanation of Ruby blocks?, that doesn't seem right.

Are blocks (Proc) objects? And thus don't break the everything is object rule in Ruby?

Community
  • 1
  • 1
Kumar
  • 117
  • 1
  • 6

1 Answers1

4

No, blocks are not (Proc) objects (although it is easy to map from one to the other).

Obviously, "everything is object in Ruby" is wrong in the absolute sense. A character is not an object. Even if limit your attention to tokens, keywords are not objects; comments are not objects; parentheses are not objects. So it is not surprising that a block is not an object.

Every proposition has contexts in which it is to be interpreted. When people say "everything is object in Ruby", the context is "with respect to what some major languages regard as atomic/scalar, such as numerals, etc."

sawa
  • 165,429
  • 45
  • 277
  • 381