DEV Community

loading...
Cover image for Sharpen your Ruby: Mastering Methods

Sharpen your Ruby: Mastering Methods

Eric The Coder
Businessman and blogger #Javascript, #Python and #PHP. My favorite frameworks are #Nextjs, #Laravel, and #Django. I am also a fan of #TailwindCSS
・4 min read

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.

Each post will include some theory but also exercise and solution.

If you have any questions/comments or your 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 programs.

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 declare with the 'def' keyword followed by the method name and parameters and finish with a 'end' keyword
  • Method parameters are specified after method name and are enclose in parentheses.
  • To invoke (call) the method you just use is name

Example:

def display_message(message)
   puts message
end

# Calling the method
display_message 'Hello World'

# or with optional parentheses
display_message('Hello World')
Enter fullscreen mode Exit fullscreen mode

Methods Return value

Ruby specifically has a unique way with 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
Enter fullscreen mode Exit fullscreen mode

That is the exact same thing as this

def addition(a, b)
   return a + b
end

puts addition 10, 5
# 15
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Since the last line is always return Ruby return the results of the puts method and thats nothing.

So there is a clear difference between returning a + b vs returning puts a + b

By convention the keyword 'return' is never use if we want to return the last line (since that's the Ruby default).

But the keyword 'return' need to be use if we want to return something before the last line:

def check(a, b)
  if a > 100
    return 'Number too high'
  end
  'Number are correct'
end

# call the method to test the result
check 120, 30 # Number too high
check 50, 2 # Number are correct
Enter fullscreen mode Exit fullscreen mode

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 than 100. The method will return 'Number are correct'. And agin, 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
Enter fullscreen mode Exit fullscreen mode

By convention methods that end with a '?' always return a boolean value (true or false).

You can create you own boolean method:

def is_valid?(password)
  if password.length > 1
    return true
  end
  false
end

# call the method
puts is_valid? 'secret'
# true
Enter fullscreen mode Exit fullscreen mode

Method name that end with a !

In Ruby some method name end with a ! Those methods are call bang methods. Bang method modify an object in-place. This can be dangerous because it change the object value and that may be not your intent.

For example Ruby have two reverse method one regular and one bang!

name.reverse
# and
name.reverse!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Since b is not specified Ruby use it default value of 10

Methods Named Arguments

Since a 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
Enter fullscreen mode Exit fullscreen mode

As you can see, with multiple arguments it can become difficult to read understand which arguments is what.

Named arguments is made for that kind of situation:

def calculation(price, shipping, taxes)
   price + shipping + taxes
end

calculation(price = 200, shipping = 50, taxes = 20) # 270
Enter fullscreen mode Exit fullscreen mode

Now the method usage 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
Enter fullscreen mode Exit fullscreen mode

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

def subtraction(a, b, c = 0)
   a - b - c
end

puts subtraction(100, 50) # 50
Enter fullscreen mode Exit fullscreen mode

Conclusion

That's it for today. The journey just started, stay tune 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.

I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman

Discussion (1)

Collapse
bcotteret profile image
Bruno Cotteret

Hi,
There are a type in the calculation methods :
It won't work as is, because the local variable "shipping_fee" is not defined,
I'm sure you meant "shipping" to match with the argument :)

Forem Open with the Forem app