Ruby block
I’ve been trying to figure out how it works. This morning I wanted to write it down my thinking process today, before I forget.
block_given?
block_given? is a method of Kernel module. In other words, it’s one of the same kind of puts
, gets
, sleep
, etc. They are at the top-level, so to speak.
The Kernel module is included by class Object, so its methods are available in every Ruby object.
And block_given?
verifies if block is given when you call a method.
What do you mean by “block is given”? In normal cases (when block is not given) the codes should be like this:
object_name.method_name(parameters)
object_name.method_name
Here you call methods with parameters, or without parameters. But when you say “block is given” (However this is also normal, nothing special) that means you called it with a block. A lot like a parameter.
object_name.method_name(parameter_names) { chunks of
...