Use arguments in ruby

Tram Ho

In the past, we learned that programming is probably very familiar with how many arguments / methods need to pass, so it will pass as many arguments even when we reuse that function in a different context, some of the arguments Unnecessary in or even some cases where the number of arguments passed back is insufficient. Ruby language is quite flexible in this, maybe in some other languages ​​too, but because of our rigidity, we forget how to use these pretty good arguments.

How to pass standard arguments (Required)

This way, as I said above, is the most familiar way. As much as we ask for it, we don’t need it

If we don’t pass a sufficient number of arguments, we get the following error message:

It means we need to pass 3 arguments, not 2.

Use optional arguments (Optional)

In some cases, if you want to call a function that has a few passed arguments, it will have a default value

Now you can just pass two file and data arguments, and the mode argument if you forget or try not to pass it will default to = “w”, and if you remember and pass another value then That value will override the default value

Use arguments with keywords (Keyword arguments)

One of the disadvantages between a function and an argument is that in order for the function to run properly, the order of arguments passed must be correct.
If we pass the arguments in the wrong order, the function may fail or the results will be incorrect.
Using arguments with keys will help us change the order of arguments passed

It helps us to call the function with different argument order:

But more importantly, it helps us know exactly which arguments we pass and how much.
I don’t usually use this type of argument, but it’s quite useful when we want to increase the clarity that in cases of passing a series of values ​​of the same type, for example:

We will not be confused with which value of x, which value of y.
That’s one of the main purposes of this type of argument: avoid confusion and misunderstand as much as possible.

Functions that do not limit the number of arguments (variable arguments)

What if you want to pass an unknown number of arguments to a function?

This type of argument allows us to pass as many arguments as we can even without any arguments. And itself is an array containing the values ​​to be passed.
We can combine this type of argument with other types of arguments

The above function will take the first value passed to the title , and the next argument will be in the array chapters. Note that this argument type must be after the required argument type (optional) and optional argument (optional), and before the key argument (keyword arguments).

Use the right order

If you want to use a combination of argument types and avoid errors, then you must use the correct argument types in the following order:

required -> optional -> variable -> keyword Here is an example that uses all types of arguments:

**x is similar to variable arguments, but will be stored as a hash instead of an array

The Catch-All argument

This means that this function accepts all of the arguments passed, but does nothing with all of those arguments, it’s similar to using the _ character in a block to denote unused arguments.
The actual use for this type of argument is with the super keyword:

Suppose you change the argument list of nutrition function in class Food , you do not need to go looking for nutrition function of class Bacon to match argument list like class Food anymore, it will be changed by default. class Food then



Share the news now

Source : Viblo