
I develop in Javascript, Python, PHP, and Ruby. By far Ruby is my favorite programming language.
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 syntax
- 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
Example:
defdisplay_message(message)putsmessageend# Calling the methoddisplay_message'Hello World'# or with optional parenthesesdisplay_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
defaddition(a,b)a+bendputsaddition10,5# 15
That is the exact same thing as this
defaddition(a,b)returna+bendputsaddition10,5# 15
Since the last line is always return, the return keyword is optional.
Attention. This can be confusing:
defaddition(a,b)putsa+bendputsaddition10,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:
defcheck(a,b)ifa>100return'Number too high'end'Number is correct'end# call the method to test the resultcheck120,30# Number too highcheck50,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=4number.even?# truenumber.odd?# false
By convention methods that end with a '?' always return a boolean value (true or false).
You can create your own boolean method:
defis_valid?(password)ifpassword.length>1returntrueendfalseend# call the methodputsis_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# andname.reverse!
The bang! method will change the value of the object in-place
name='Mike'putsname.reverse!# ekiM# that method bang! will have also update the name variableputsname# ekiM
Methods arguments default value
It is possible to set default value for method parameter
defaddition(a,b=10)a+bendaddition100# 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:
defcalculation(price,shipping,taxes)price+shipping_fee+taxesendcalculation(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:
defcalculation(price,shipping,taxes)price+shipping+taxesendcalculation(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
Exercise
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
Solution
defsubtraction(a,b,c=0)a-b-cendputssubtraction(100,50)# 50
Conclusion
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_
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse