home PYTHONJAVA
 

Ruby block

You already know how Ruby defines methods and how you call methods. Similarly, Ruby has a block concept.

    The
  • block consists of a lot of code.
  • You need to give the block a name.
  • The code in the block is always enclosed in braces {}.
  • A block is always called from a function with the same name. This means that if your block name is test, then you will call this block using the function test.
  • You can use the yield statement to call a block.

grammar

block_name{ statement1 statement2 .......... }

Here, you will learn how to call a block using a simple yield statement. You will also learn how to call a block using the yield statement with parameters. In the example, you will see both types of yield statements.

yield statement

Let's look at an instance of a yield statement:

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test puts "Within the test method" yield puts "You are back in the test method" yield end test {puts "You are in the block"}

The above example runs as follows:

in test method< /span>
You are inside the block
You are back  test in-method
You are inside the block

You can also pass a yield statement with parameters. Here's an example:

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- def test yield 5 puts "Within the test method" yield 100 end test {|i| puts "You are in the block #{i} Inside"}

The above example runs as follows:

You are in the block 5< /span> Inside
in test in-method
You are in the block 100 Inside

Here, the yield statement is followed by the argument. You can even pass multiple parameters. In the block, you can place a variable between the two vertical lines to accept the parameters. Therefore, in the above code, the yield 5 statement passes the value 5 as a parameter to the test block.

Now, look at the following statement:

test {|i| puts "You are in block #{i}"}

Here, the value 5 will be received in the variable i. Now, observe the puts statement below:

puts " You are in block #{i}"

The output of this puts statement is:

You are in the block5 Inside

If you want to pass multiple parameters, the yield statement looks like this:

yield a , b

At this point, the block looks like this:

test {|a, b| statement}

Parameters are separated by commas.

Blocks and methods

You have seen how the blocks and methods are related to each other. You usually use a yield statement to call a block from a method with the same name. So the code looks like this:

Instance

#!/usr/bin/ruby def test yield end test{ puts "Hello world"}

This example is the easiest way to implement a block. You call the test block with the yield statement.

But if the last parameter of the method is preceded by &, then you can pass a block to the method, and this block can be assigned to the last parameter. If * and & appear in the parameter list at the same time, & should be placed after.

div class="example">

Instance

#!/usr/bin/ruby def test(&block) block.call end test { puts "Hello World!"}

The above example runs as follows:

Hello World!

BEGIN and END blocks

Each Ruby source file can declare the block of code (BEGIN block) to run when the file is loaded, and the block of code (END block) to run when the program completes execution.

Instance

#!/usr/bin/ruby BEGIN { # BEGIN Code Block puts "BEGIN code block" } END { # END code block puts "END Code Block" } # MAIN Code Block puts "MAIN code block"

A program can contain multiple BEGIN and END blocks. BEGIN blocks are executed in the order in which they appear. The END blocks are executed in the reverse order in which they appear. When executed, the above program outputs the following results:

BEGIN code block
MAIN code block
END code block





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.