YouTip LogoYouTip

Ruby Block

# Ruby Blocks You already know how Ruby defines methods and how you call them. Similarly, Ruby has the concept of blocks. * Blocks consist of a large amount of code. * You need to give the block a name. * The code in the block is always enclosed within curly braces `{}`. * Blocks are always called from a function with the same name. This means if your block is named _test_, you use the function _test_ to call this block. * You can use the _yield_ statement to call a block. ## Syntax 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 a _yield_ statement with parameters. In the examples, you will see both types of _yield_ statements. ## _yield_ Statement Let's look at an example of the yield statement: ## Example #!/usr/bin/ruby# -*- coding: UTF-8 -*-def test puts"Inside test method"yield puts"Back inside test method"yield end test {puts"Inside the block"} [Try it Β»](#) The output of the above example is: Inside test methodInside the blockBack inside test methodInside the block You can also pass a yield statement with parameters. Here is an example: ## Example #!/usr/bin/ruby# -*- coding: UTF-8 -*-def test yield 5 puts"Inside test method"yield 100 end test {|i| puts"You are in block #{i}"} [Try it Β»](#) The output of the above example is: You are in block 5Inside test methodYou are in block 100 Here, the _yield_ statement is followed by a parameter. You can even pass multiple parameters. In the block, you can place a variable between two vertical bars to accept 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 is received in the variable i. Now, observe the following puts statement: puts"You are in block #{i}" The output of this puts statement is: You are in block 5 If you want to pass multiple parameters, the _yield_ statement looks like this: yield a, b Then the block looks like this: test {|a, b| statement} Parameters are separated by commas. ## Blocks and Methods You have already seen how blocks and methods are interrelated. You typically use the yield statement to call a block from a method with the same name. So, the code looks like this: ## Example #!/usr/bin/ruby def test yield end test{ puts"Hello world"} This example is the simplest way to implement a block. You use the _yield_ statement to call the test block. However, if the last parameter of a method is preceded by `&`, you can pass a block to that method, and this block can be assigned to the last parameter. If both `*` and `&` appear in the parameter list, `&` should be placed last. ## Example #!/usr/bin/ruby def test(&block)block.call end test { puts"Hello World!"} [Try it Β»](#) The output of the above example is: Hello World! ## BEGIN and END Blocks Each Ruby source file can declare a block of code to be run when the file is loaded (a BEGIN block) and a block of code to be run after the program has finished executing (an END block). ## Example #!/usr/bin/ruby BEGIN { # BEGIN block puts"BEGIN block" } END { # END block puts"END block" } # MAIN block puts"MAIN block" A program can contain multiple BEGIN and END blocks. BEGIN blocks are executed in the order they appear. END blocks are executed in the reverse order they appear. When executed, the above program outputs the following: BEGIN block MAIN blockEND block
← Ruby ModuleRuby Method β†’