Passing Arguments

Learn how to pass arguments to a method in Ruby.

We'll cover the following

Extra information needed

An object occasionally needs extra information to do what we ask.

For example, the String class defines the delete method, which returns another string with some characters deleted. To do so, it needs to know which characters we’d like to remove.

We can pass things by appending parentheses () to the method call (the name). We can then include the extra bit of information needed (in our case, another string) inside the parentheses, like so:

Press + to interact
puts name = "Ruby Monstas"
puts name.delete("by Mo")

Another example of a method that needs an argument is the prepend method, used on strings:

Press + to interact
puts name = "Ruby Monstas"
puts name.prepend("Oh, hello, ")

These extra bits of information are called arguments. We’ll discuss them more once we define our own methods.

Not all methods need these extra bits of information (arguments) to do their job, such as the length method on strings.

How do we know if a method requires an argument?

Once again, memorizing all of them is unnecessary. We can simply check the documentation when needed.