Ruby Language for Beginners Part 2 - Ruby Methods

Ruby Language for Beginners Part 2 - Ruby Methods


This is a post from the Ruby Language for Beginners in 8 Parts!

In the today's post we're going to see Methods and Variables in Ruby

Ruby Methods

Let's create the following math calculation:

puts itself is a Ruby method. Actually it's a built-in Ruby method and we can create our own Ruby methods:

We could create a calculator to sum numbers by creating a method called sum, receiving 2 parameters:

Notice the pattern to create a Ruby method:

Ruby is really smart and you don't need to use the return keyword:

Ruby evaluates the last expression and returns that : )

Just remembering, we don't need to use parentheses when calling a method:

Let's jump into Ruby Variables!

Ruby Variables

We need to keep values in some place, right? : )

Let's create a method to sum up 10 to a given number and then return the result:

As you can see we created the variable ten to hold the value 10

Can we change the variable in the middle of the program? Yes, we can!

The output will be:

10 15

Changing the type of the variable

Ruby has another important characteristics: You can change the type of the variable, even after initiated it.

In the previous example we had the Ruby code:

This is totally fine in Ruby!

That's it! In the next post: Part 3 - Ruby Strings we're going to see Ruby Strings!

I hope that would be useful to you! Thanks!

Follow us to keep up to date!

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Ruby Beginners Tutorial - Ruby Features and Characteristics - Hacking Code

Part 1 - Ruby Language Characteristics

This is the first Post of the Ruby Series! Let’s see the first Ruby Code using IRB!

Ruby Beginners Tutorial - Ruby Methods - Hacking Code

Part 2 - Ruby Methods and Variables

How can we work with Methods and Variables in Ruby? Let’s see!

Ruby Beginners Tutorial - Ruby Strings - Hacking Code

Part 3 - Ruby Strings

It’s really easy to work with Strings in Ruby! Let’s see how!

Ruby Beginners Tutorial - Ruby Objects - Hacking Code

Part 4 - Ruby Classes and Objects

You can’t work without Classes and Objects in Ruby! It’s time to learn how to create them!

Ruby Beginners Tutorial - Ruby Conditionals - Hacking Code

Part 5 - Ruby Conditionals

Conditionals are important in every Programming Language! But how does that work in Ruby?

Ruby Beginners Tutorial - Ruby Arrays - Hacking Code

Part 6 - Ruby Arrays

Arrays in Ruby is really easy and has a lot of great methods! Check it out!

Ruby Beginners Tutorial - Ruby Hashes - Hacking Code

Part 7 - Ruby Hashes

Hashes in Ruby can be used in different scenarios! Let’s see how to use it!

Ruby Beginners Tutorial - Ruby Loops - Hacking Code

Part 8 - Ruby Loops

Ruby Loops to iterate through Arrays and Hashes is really great! Let’s see why!



  1. Ruby Language for Absolute Beginners in 8 Parts | Hacking Code - […] Part 2 - Ruby Methods and Variables […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: