In this program, we will demonstrate the yield statement with an argument. The yield statement is used to call block inside the method with a specified argument.
The source code to demonstrate the yield statement with the argument is given below. The given program is compiled and executed successfully.
# Ruby program to demonstrate the
# yield statement with argument.
def MyMethod
puts "Inside the method"
yield " by Rahul"
puts "Hello from MyMethod"
yield " by Rohit"
end
MyMethod{|arg|puts "Hello from Block #{arg}"}
Output:
Inside the method
Hello from Block by Rahul
Hello from MyMethod
Hello from Block by Rohit
Explanation:
In the above program, we used the "yield" statement with the argument to call block inside the method and printed the appropriate message.
Program/Source Code:
The source code to demonstrate the yield statement with the argument is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we used the "yield" statement with the argument to call block inside the method and printed the appropriate message.
need an explanation for this answer? contact us directly to get an explanation for this answer