Ruby block


Release date:2023-10-27 Update date:2023-10-28 Editor:admin View counts:397

Label:

Ruby block

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

  • Blocks are made up of a lot of code.

  • You need to give the block a name.

  • The code in the block is always contained in curly braces {} inside.

  • A block is always called from a function with the same name as it. This means that if your block name is test then you want to use the function test to call this block.

  • You can use the yield statement to invoke the block.

Grammar

block_name{statement1statement2.......... }

Here, you will learn how to use a simple yield statement to invoke the block. You will also learn how to use the yield statement to invoke theblock. In the example, you will see these two types of yield statement.

yield statement

Let’s see one yield example of the statement:

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftestputs"In the test method, "yieldputs" you
return to the test method, "yieldendtest {puts" you are in the block "}

The running result of the above instance is as follows:

Within the test method
You're in the block
You're back in the test method again
You're in the block

You can also pass a yield statement. Here is an example:

Example

#!/usr/bin/ruby#-*- coding: UTF-8 -*-deftestyield5puts"
In the test method, 'yield100endtest {|i|puts' you are in block #{i}'}

The running result of the above instance is as follows:

You're within block 5
Within the test method
You're within 100 blocks

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

Now, look at the following statement:

test{\|i\|puts"You are within block # {i}“}

In this case, the value of 5 will be in the variable i copy that in. Now, look at the following puts statement:

puts"You are within block # {i}“"

This puts output of the statement is:

You are within block # {i}“

If you want to pass multiple parameters, then yield statement is asfollows:

yielda,b

At this point, the block is as follows:

test{\|a,b\|statement}

Parameters are separated by commas.

Blocks and methods

You have seen how blocks and methods relate to each other. You usually use the yield statement invokes a block from a method with the same name. Therefore, the code is as follows:

Example

#!/usr/bin/rubydeftestyieldendtest{puts"Hello world"}

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

But if the last parameter of the method is preceded by a & then you canpass a block to the method, and the block can be assigned to the last parameter If * and & are also appears in the parameter list & .It should be in the back.

Example

#!/usr/bin/rubydeftest(&block)block.callendtest{puts"Hello World!"}

The running result of the above instance is as follows:

Hello World!

BEGIN and END blocks

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

Example

#/ Usr/bin/rubyBEGIN {# 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 block. BEGIN Blocks are executed in the order in which they appear. END blocks are executed in the reverse order in which they appear. When executed, the aboveprogram outputs the following results:

BEGIN block
MAIN block
END block

Powered by TorCMS (https://github.com/bukun/TorCMS).