Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Ruby is a general-purpose, dynamic, reflective, object-oriented programming language. Everything in Ruby is an object. Ruby's development aimed to create a user interface between human programmers and the underlying computational machinery.
Methods in Ruby is a set of statements that returns a result. With the help of methods, one may organize their code into simple-to-use subroutines that can be called from different parts of the program.
As there are closures in different programming languages, Ruby have blocks. A block in Ruby is similar to a method but does not belong to an object.
Let’s learn how to pass arguments in methods and blocks in Ruby.
Passing Arguments to Methods
Let’s understand how to pass arguments to a method with the help of examples.
Example 1:
# Ruby program to show how to pass arguments to a Method
# method
def ninjas(designation1, designation2)
# method statements to be executed
puts "Ninjas are #{designation1}!"
puts "Ninjas are #{designation2}!"
end
# calling method ninjas
ninjas("Coders", "Developers")
You can also try this code with Online Ruby Compiler
In the above example, we can see that we are passing two arguments into our method ninjas and using those arguments in the method statements to execute it.
Example 2:
# Ruby program to show how to pass arguments to a Method
# method
def func(n)
count = 1;
while count<=n
# statements to be executed
puts "Coding Ninjas"
count = count+1;
# while loop ends here
end
end
# calling method func
func(5)
You can also try this code with Online Ruby Compiler
In the above example, we can see that we are passing an argument into the method func and using that argument in the while loop to execute the desired statement.
Passing Arguments to Blocks
With the help of the pipe ( || ) symbol, we may pass the block with certain arguments.
Let’s understand this with the help of examples.
Example1:
Creating a block where we pass an argument to a Block.
We have passed a logic on the list to select only even numbers from the list. We can see we are getting only even numbers from the list as our output.
Passing Arguments to a Method using Block
We can provide arguments to yield (which will be given to the block) and act upon the yield statement's value (this is the value of the last statement in the block).
Here is an example of a method that uses the value of the code block and accepts arguments for its block:-
# Ruby program to show a method that uses the value of the code block and accepts arguments for its block.
# method
def ninjas
var1 = yield("Coders")
puts "Ninjas are best #{var1}!"
var2 = yield("Developers")
puts "Ninjas are best #{var2}!"
end
# block
ninjas do |designation|
puts "Ninjas are #{designation}!"
designation == "Coders" ? "Coders in the World" : "Developers in the World"
end
You can also try this code with Online Ruby Compiler
In the above program, we can see that we are passing an argument to the yield statement and that arguments we can again use as a variable to execute the method statement.
Frequently Asked Questions
What are Objects in Ruby?
In Ruby, everything is an object. All objects have a unique identification; they can also maintain a state and exhibit behavior in response to messages. Usually, these messages are sent out via method calls. A string is an example of a Ruby object.
What are Methods in Ruby?
A Ruby method is a set of statements that returns a result. With the help of methods, one may organize their code into simple-to-use subroutines that can be called from different parts of their program.
What is a block in Ruby?
In Ruby, a block is similar to a method. Ruby blocks enable us to execute any calculations and manipulation in the same manner as we would inside of any method. Therefore, we can say that while blocks in Ruby are identical to any method, they do not belong to any objects.
What are the main types of blocks in Ruby?
There are mainly three types of blocks in Ruby:-
1.) do and end statement blocks.
2.) Inline blocks or with Curly braces.
3.) Block with arguments.
What do we need to write a method that accepts a block?
You need to do nothing specific to enable your method to accept a block. If the caller passes one in, any method can use it. You may call the block with the yield keyword at any point in your method.
Conclusion
In this article, we have extensively discussed how to pass arguments to methods and blocks in Ruby with the help of code examples.