home PYTHONJAVA
 

Ruby block

You definitely realize how Ruby characterizes techniques and how you call strategies. Correspondingly, Ruby has a square idea.

    The
  • block comprises of a great deal of code.
  • You need to give the square a name.
  • The code in the square is constantly encased in supports {}.
  • A square is constantly called from a capacity with a similar name. This implies on the off chance that your square name is test, at that point you will call this square utilizing the capacity test.
  • You can utilize the yield proclamation to call a square.

grammar

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

Here, you will figure out how to call a square utilizing a basic yield articulation. You will likewise figure out how to call a square utilizing the yield explanation with parameters. In the precedent, you will see the two sorts of yield explanations.

yield statement

Let's take a gander at a case 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 isolated by commas.

Blocks and methods

You have perceived how the squares and strategies are identified with one another. You more often than not utilize a yield proclamation to call a square from a strategy with a similar name. So the code looks like this:

Instance

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

This model is the most straightforward approach to actualize a square. You call the test obstruct with the yield explanation.

But on the off chance that the last parameter of the technique is gone before by &, at that point you can pass a square to the strategy, and this square can be doled out to the last parameter. On the off chance that * and & show up in the parameter list in the meantime, & ought to be put after.

div class="example">

Instance

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

The above precedent keeps running as follows:

Hello World!

BEGIN and END blocks

Each Ruby source document can announce the square of code (BEGIN obstruct) to run when the record is stacked, and the square of code (END hinder) to run when the program finishes execution.

Instance

#!/usr/container/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 numerous BEGIN and END squares. Start squares are executed in the request in which they show up. The END squares are executed in the invert request in which they show up. Whenever executed, the above program yields the accompanying results:

BEGIN code block 

Fundamental 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.