Ruby code blocks are called closures in other programming languages. It consist of a group of codes which is always enclosed with braces or written between do..end. The braces syntax always have the higher precedence over the do..end syntax. Braces have high precedence and do has low precedence.
A block is written in two ways,
- Multi-line between do and end (multi-line blocks are niot inline)
- Inline between braces {}
Both are same and have the same functionality.
To invoke a block, you need to have a function with the same name as the block.
A block is always invoked with a function. Blocks can have their own arguments.
syntax:
block_name{
statement1
statement2
..........
}
Example:
The below example shows the multi-line block.
[10, 20, 30].each do |n|
puts n
end
Output:
Below example shows the inline block.
[10, 20, 30].each {|n| puts n}
Output:
The yield statement
The yield statement is used to call a block within a method with a value.
Example:
- #!/usr/bin/ruby
-
- def met
- puts "This is method"
- yield
- puts "You will be back to method"
- yield
- end
- met {puts "This is block"}
Output:
While the execution of met method, when we reach at yield line, the code inside the block is executed. When block execution finishes, code for met method continues.
Passing parameters with yield statement
One or more than one parameter can be passed with the yield statement.
Example:
#!/usr/bin/ruby
def met
yield 1
puts "This is method"
yield 2
end
met {|i| puts "This is block #{i}"}
Output:
Block Variables
We can use same variable outside and inside a block parameter. Let’s see the following example.
Example:
#!/usr/bin/ruby
x = "Outer variable"
3.times do |x|
puts "Inside the block: #{x}"
end
puts "Outside the block: #{x}"
Output:
In this example, we are using same variable inside the block as the block parameter x and outside the block as a variable x.
BEGIN and END block
Ruby BEGIN and END block is used to declare that file is being loaded and file has been loaded respectively.
Example:
#!/usr/bin/ruby
BEGIN {
puts "code block is being loaded"
}
END {
puts "code block has been loaded"
}
puts "This is the code block"
Output:
Ampersand parameter (&block)
The &block is a way to pass a reference (instead of a local variable) to the block to a method.
Here, block word after the & is just a name for the reference, any other name can be used instead of this.
Example:
def met(&block)
puts "This is method"
block.call
end
met { puts "This is &block example" }
Output:
Here, the block variable inside method met is a reference to the block. It is executed with the call mehtod. The call method is same as yield method.
Initializing objects with default values
Ruby has an initializer called yield(self). Here, self is the object being initialized.
Example:
class Novel
attr_accessor :pages, :category
def initialize
yield(self)
end
end
novel = Novel.new do |n|
n.pages = 564
n.category = "thriller"
end
puts "I am reading a #{novel.category} novel which has #{novel.pages} pages."
Output:
Leave a Reply