Sharpen your Ruby: Part 4
Tags: ruby, rails, beginners, tutorial
Published: 2021-08-27 18:59:42

---
title: Sharpen your Ruby: Part 4
published: true
description: Learn Ruby Programming Language
tags: ruby, rails, beginner, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypw5698bi8gw3d19hl5v.jpeg
series: learn-ruby
canonical_url: https://eric-the-coder.com/sharpen-your-ruby-part-4
---

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: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> 

If you have any questions/comments or you are new and need help, you can comment below or send me a message.

<h2>Whats is a Method?</h2>
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.

<h3>Method syntax</h3>
- 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:
```ruby
def display_message(message)
   puts message
end

# Calling the method
display_message 'Hello World'

# or with optional parentheses
display_message('Hello World')
```

<h3>Methods Return value</h3>
Ruby specifically has a unique way of working with returned values. 

Ruby automatically return the last line of the method
```ruby
def addition(a, b)
   a + b
end

puts addition 10, 5
# 15
```
That is the exact same thing as this
```ruby
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:
```ruby
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:
```ruby
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.

<h3>Method name that end with a ?</h3>
In Ruby some method name end with a ?
```ruby
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:
```ruby
def is_valid?(password)
  if password.length > 1
    return true
  end
  false
end

# call the method
puts is_valid? 'secret'
# true
```
<h3>Method name that end with a !</h3>
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!
```ruby
name.reverse
# and
name.reverse!
```
The bang! method will change the value of the object in-place
```ruby
name = 'Mike'
puts name.reverse! # ekiM

# that method bang! will have also update the name variable
puts name
# ekiM
```

<h3>Methods arguments default value</h3>
It is possible to set default value for method parameter
```ruby
def addition(a, b = 10)
   a + b
end

addition 100
# 110
```
Since b is not specified Ruby use it default value of 10

<h3>Methods Named Arguments</h3>
Since an image is worth a thousand words let look at this example:
```ruby
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:
```ruby
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.
```ruby
calculation(taxes = 20, shipping = 50, price = 200) # 270
```

<h1>Exercise</h1>
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
```ruby
def subtraction(a, b, c = 0)
   a - b - c
end

puts subtraction(100, 50) # 50
```

<h3>Conclusion</h3>
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: <a href="https://twitter.com/EricTheCoder_" class="twitter-follow-button" data-show-count="false">EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>