Ruby Language for Beginners Part 3 - Ruby Strings

Ruby Language for Beginners Part 3 - Ruby Strings

Hello!

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

In the today's post we're going to see Ruby Strings

Ruby Strings

A Ruby string can be created using quotes. You starts the string with a quote and finishes the string with a quote.

Example: "My String in Ruby"

1 - Concatenating Strings

2 - Variable with Strings

3 - Concatenating different types in Ruby

Let's try to concatenate a number with a string:

If we execute:

In Ruby we must convert it first. Let's use the "to_s" method from the Fixnum object when, in this case, is the variable number

4 - Interpolating Strings

Sometimes we can have the following code:

The output will be:

Hacking Code Tutorials: Java, Ruby, Go, Docker

But it's easy to get lost in this String concatenation. In this case is better to use String Interpolation:

Much better, isn't it?

5 - String Interpolation Evaluating Expression

We can evaluate expressions with Ruby Interpolation

And the result will be 15

Important: This works since we're using double quotes in the String interpolation!

With single-quoted strings, Ruby will just understand the string literally

The output will be the following, without the interpolation:

So, we can use single quotes when we need to use the literal strings!

That's it! In the next post: Part 4 - Ruby Classes, Objects and Instances we're going to see Ruby Classes, Objects and Instance!

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!

0 Comments

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: