Together let start a journey and revisit our Ruby foundations.
Follow me on Twitter: EricTheCoder_
If you have any questions/comments or you are new and need help, you can comment below or send me a message.
Whats is a Method?
Methods are a powerful feature for building Ruby programs, they allow you to encapsulate behavior and call the method later on to build a full program.
- Method name must start with a letter. It may contain letters, numbers, an _ (underscore or low line).
- The convention is to use underscores to separate words in a multiword method name
- Method is declared with the 'def' keyword followed by the method name and parameters and finish with an 'end' keyword
- Method parameters are specified after the method name and are enclosed in parentheses.
- To invoke (call) the method you just use is name
def display_message(message) puts message end # Calling the method display_message 'Hello World' # or with optional parentheses display_message('Hello World')
Methods Return value
Ruby specifically has a unique way of working with returned values.
Ruby automatically return the last line of the method
def addition(a, b) a + b end puts addition 10, 5 # 15
That is the exact same thing as this
def addition(a, b) return a + b end puts addition 10, 5 # 15
Since the last line is always return, the return keyword is optional.
Attention. This can be confusing:
def addition(a, b) puts a + b end puts addition 10, 5 # 15 # empty
Since the last line always returns Ruby return the results of the puts method and that's nothing.
So there is a clear difference between returning a + b vs returning puts a + b
By convention, the keyword 'return' is never used if we want to return the last line (since that's the Ruby default).
But the keyword 'return' need to be used if we want to return something before the last line:
def check(a, b) if a > 100 return 'Number too high' end 'Number is correct' end # call the method to test the result check 120, 30 # Number too high check 50, 2 # Number are correct
This method will return 'Number too high' if variable 'a' is greater than 100. After the return the method will end. So the last line will never be executed.
If variable 'a' is less or equal to 100. The method will return 'Number is correct'. And again, since it is the last line of the method the 'return' keyword is optional.
Method name that end with a ?
In Ruby some method name end with a ?
number = 4 number.even? # true number.odd? # false
By convention methods that end with a '?' always return a boolean value (true or false).
You can create your own boolean method:
def is_valid?(password) if password.length > 1 return true end false end # call the method puts is_valid? 'secret' # true
Method name that end with a !
In Ruby some method names ends with a ! Those methods are call bang methods. Bang method modifies an object in place. This can be dangerous because it changes the object value and that may be not your intent.
For example, Ruby has two reverse methods one regular and one bang!
name.reverse # and name.reverse!
The bang! method will change the value of the object in-place
name = 'Mike' puts name.reverse! # ekiM # that method bang! will have also update the name variable puts name # ekiM
Methods arguments default value
It is possible to set default value for method parameter
def addition(a, b = 10) a + b end addition 100 # 110
Since b is not specified Ruby use it default value of 10
Methods Named Arguments
Since an image is worth a thousand words let look at this example:
def calculation(price, shipping, taxes) price + shipping_fee + taxes end calculation(200, 50, 20) # 270
As you can see, with multiple arguments it can become difficult to read understand which arguments is what.
Named arguments are made for that kind of situation:
def calculation(price, shipping, taxes) price + shipping + taxes end calculation(price = 200, shipping = 50, taxes = 20) # 270
Now the method used is clearer.
Another good thing about named arguments is that you can change the order of the arguments.
calculation(taxes = 20, shipping = 50, price = 200) # 270
Create a little program that:
- Create a method name subtraction with 3 arguments
- That method with return the result of subtraction of the 3 numbers pass as arguments.
- If the last argument is not specified it will be treated as default value of 0
- Call that method and print its result
def subtraction(a, b, c = 0) a - b - c end puts subtraction(100, 50) # 50
That's it for today. The journey just started, stay tuned for the next post very soon. (later today or tomorrow)
If you have any comments or questions please do so here or send me a message on Twitter.
Follow me on Twitter: EricTheCoder_