mszub
mszub

Reputation: 15

How do method arguments work in Ruby?

I can't seem to find an answer to this question, and the two books I've checked seem to be written for experienced programmers who are new to Ruby, not beginners to programming as well as Ruby. I'm struggling to understand how method arguments work and what their purpose is. For example, this I can understand:

def method_name
  # method behaviour
end

Then, whenever I want to re-use that particular batch of code, I simply re-enter method_name and Ruby will repeat the actions as specified in that method. However, most methods I've seen include arguments in parenthesis immediately following the name, i.e.

def method_name (argument1, argument2)
  # method behaviour
end

I'm struggling to grasp what the arguments mean and how they factor into the functionality of the code. Also, why are arguments required for some methods but not others? Do arguments represent input from the user? Are they similar to params?

Upvotes: 0

Views: 115

Answers (3)

Sagar Pandya
Sagar Pandya

Reputation: 9497

Methods have the option of taking arguments and it's up to you the designer whether or not to include this option. Sometimes it just makes sense to include this option, sometimes not. You will see this as you code more.

Example without argument

def say_hello
  puts "hello"
end

#calling the method
say_hello #=> hello

This method is simply designed to say hello when called, which works fine.

Example with argument

def say_personal_hello(name)
  puts "hello #{name}"
end

#calling the method with an argument
say_personal_hello("mszub") #=> hello mszub

But this method takes an argument because I want it to be personalised by putting a particular name in it. The name can be a user input if you like for example:

puts "What's your name?"
input_name = gets.chomp

def say_personal_hello(input_name)
  puts "hello #{input_name}"
end

say_personal_hello(input_name)

Hope this helps. Good luck.

Upvotes: 0

Leo Correa
Leo Correa

Reputation: 19789

Function arguments don't just apply to Ruby.

Think for example a mathematical function f(x) = x^2, where x is the input and the function simply uses that input to return an output, in this case x squared.

This concept applies to programming across many languages where arguments can be used to specify a contract between the function and the caller of the function to modify the behavior of the function.

In ruby the above mathematical function could be defined as:

def square(x)
  x ** 2
end 

square(3) #=> 9
square(4) #=> 16

Upvotes: 0

Jeff F.
Jeff F.

Reputation: 1067

Like you said, think of the arguments like parameters (the two words are used for slightly different meanings but it's not important for your understanding at this point.)

The arguments are input to be used in the method behavior.

def add(arg1, arg2)
    sum = arg1 + arg2
    return sum
end

Then you can call that method with some arguments:

result = add(1, 2)
# result equals 3

Upvotes: 2

Related Questions