Updated April 5, 2023
Introduction to Yield in Ruby
In Ruby if we wanted to call any method with the block code then we can use the yield statement. yield is a keyword in Ruby which allow the developer to pass some argument to block from the yield, the number of the argument passed to the block has no limitations, the main advantage of using yield in Ruby, if we face any situation we wanted to our method perform different functions according to calling block, which means we can define some logic inside the block and when the method gets called the logic block will execute, with help of this behavior anyone can call to the method with different block according to the requirement.
Syntax
Below is a very simple syntax for the yield statement. In the below syntax we have written two yields inside the method_name and each yield contains the argument value for the block, which means yield will pass the argument value to the block or in the more general word we can say, each time block will get a call and it gets the value passed from the yield block.
Please see the below syntax.
def method_name
yield argumentValue1
yield argumentValue2
end
method_name {|argument| "This is the block side"}
How Yield statement works In Ruby?
We can explain the working of the yield in Ruby with the help of the following statements.
- Yield is a keyword in Ruby and when we want to make a call to any block then we can use the yield, once we write the yield inside any method it will assume for a blocking call.
- There is no limitation for passing a number of arguments to the block from yield statements.
- As many times we will write the yield inside the method it will try to call block.
- To pass any argument from yield to the block we can write || inside the block as the name of the argument.
- {|number| puts “Your message”} and yield will call as yield 5. So here 5 will be the value for the |number|.
Examples to Implement Yield in Ruby
Below are some examples mentioned:
Example #1
In the below example we have defined a method called yieldExample and inside this method we have written some puts statement and the yield statement. In this example we are directly printing the when calling yieldExamle method without passing any argument to it. On the first time it will call and print the method contents and next it will print the block code used at the time of calling the method.
Code:
#Defining a method and inside the method, we have written yield statements. These yield statements contain messages as arguments which will be printed at the time of the call of these methods.
def yieldExample
puts "Here , you are inside the method"
yield
puts "Again you fall under method block"
yield
end
yieldExample {puts "This is the block of the code"}
Output:
Example #2
In the below example we have defined a method called yieldExample and inside this method we have written some puts statement and the yield statement. In this example we are calling the method with argument as the number and printing the number when calling yieldExamle method without passing any argument to it. When the Yield example method gets called it will print the output written inside the method calling block and block will read the number variable of its block from the yield statements.
Code:
#Defining a method and inside the method we have written yield statement these yield statements contain some integer value as the arguments which will be printed at the time of call of this method.
def yieldExample
yield 5
puts "You are inside the method yieldExample"
yield 100
end
yieldExample {|i| puts "This is the iteration number #{i}"}
Output:
Example #3
In the below example we have defined a method called yieldExample and inside this method we have written some puts statement and the yield statement. In this example we are calling the method with the argument as the name of some users and printing the names when calling the yieldable method without a block of the message. When the Yield example method gets called it will print the output written inside the method calling block and block will read names variable from the yield statements.
Code:
#Defining a method and inside the method, we have written yield statements. These yield statements contain some names as arguments which will be printed at the time of the call of these methods.
def yieldExample
yield "Ranjan Kumar Pandey"
puts "Inside the method yieldExample"
yield "Ajay Sharma"
end
yieldExample {|name| puts "Your name is #{name}"}
Output:
Example #4
In the below example we have defined a method called yieldExample and inside this method we have written some puts statement and the yield statement. This is one totally different example as here we are passing some argument to yield. By using this method we are able to perform multiple arithmetic operations with a single method. Each time when the arithmetic method gets called from the block its yield method will read data passed from || operators inside the block.
Code:
#Defining a method and inside the method, we have written yield as the method which has two parameters.
def arithmetic(a, b)
yield(a, b)
end
puts "The sum of the two numbers is #{arithmetic(8, 2) { |a, b| a + b }}" # addition of two number
puts "The multiplication of the two numbers is #{arithmetic(8, 2) { |a, b| a * b }}" # multiplication of two numbers
puts "The subtraction of the two numbers is #{arithmetic(8, 2) { |a, b| a - b }}" # subtraction of two numbers
puts "The division of the two numbers is #{arithmetic(8, 2) { |a, b| a / b }}" # division of two numbers
Output:
Conclusion
From these tutorials we learned about the basics of the yield in Ruby, we also learned about the working of the yield, we came to know that we can use yield when we wanted to call a block from any method and yield can be passed many arguments to the block.
Recommended Articles
We hope that this EDUCBA information on “Yield in Ruby” was beneficial to you. You can view EDUCBA’s recommended articles for more information.