Sharpen your Ruby: Part 2
Tags: ruby, rails, beginners, tutorial
Published: 2021-08-23 10:49:51

---
title: Sharpen your Ruby: Part 2
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-2
---

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> 

<h2>You want to sharpen your Ruby?</h2> 
In this series, we will start from the beginning and will discover every aspect of Ruby one step at a time. 

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.

<h2>Strings Declaration</h2>
String variable represents any text data. A key point for the syntax of strings declaration is that they have to be enclosed in single or double-quotes. The program will throw an error if they are not wrapped inside quotation marks.
```ruby
# Bad
name = Mike # Will throw an error

# Good
name = 'Mike'

# Good
name = "Mike"
```

Number can also be represent as string
```ruby
text_age = "45"
number_age = 45
```
The variable text_age is a string variable. It cannot be processed in Ruby as a number like be used in addition or multiplication etc. 

The number_age variable is an integer number so that variable can be part of any Ruby number manipulations methods.

<h2>String concatenation</h2>
String can be add together like this
```ruby
message = 'Hello' + ' ' + 'World'
puts message # Hello World
```

Attention. You cannot add string and number
```ruby
age = 25
name = 'Mike'
message = name + ' is ' + age + ' years old'
puts message # ERROR
```
That code returns an error because Ruby cannot add number to string. 

To make this code work, we have to convert the age variable to string using the to_s method
```ruby
message = name + ' is ' + age.to_s + ' years old'
puts message # Mike is 25 years old
```

<h2>String Interpolation</h2>
String interpolation is replacing placeholders with values in a string literal. 

For string interpolation to work. String has to be wrapped inside a double quotation mark. Here an example
```ruby
name = 'Mike'
message = "Hello #{name}" 
# Hello Mike
```
If the last code snippet, the message variable will be processed by Ruby before assignment. The #{name} placeholder will be replaced by the containing variable value.

Inside placeholder #{} any Ruby expression can be used...
```ruby
age = 45
message = "Your age in 2 years will be #{age + 2}"
# Your age in 2 years will be 47
```

<h2>In Ruby everything is an object!</h2>
You maybe have ear this before. What does that mean for us the developer?

First, what is an object? An object refers to a particular instance of a class with its own methods and properties.

In Ruby types are defined as classes, so for example, if you have a string variable, it's an instance of the String class.

For example, take this variable
```ruby
message = "Hello World"
```
This 'message' variable will be dynamically typed by Ruby as a string. That string is a class. So message is an instance of the class string. 

In Ruby the String class already has many methods to help do basic and advance string manipulations.

That also means that the 'message' variable will inherit all the methods and properties of his parent class (String).

Example of method call (syntax: object.method)
```ruby
name = 'Mike'
puts name.upcase
# MIKE
```
'upcase' is a method of the String class. This method converts all the string characters to uppercase.

For now, if you don't understand all that class instance thing THAT'S NORMAL! We will cover class and object later. 

The only thing we need to understand for now is that variables like a string variable have methods we can call to automatically do some stuff.

Here are some string methods available in Ruby. 
```ruby
puts 'Mike'.upcase
# MIKE

puts 'Mike'.downcase
# mike

puts 'mike'.capitalize
# Mike

puts 'Mike'.reverse
# ekiM

name = 'Mike Taylor'
puts name.length
# 11

name = ''
puts name.empty?
# true

name = 'Mike'
puts name.include? 'ke'
# true
```
Look how those methods names are self-descriptive. We do not need any comments and any explanation to understand what each method does... Welcome to Ruby world!

<h2>More String methods</h2>
Now that we understand the basics we will start to learn more advanced string methods. 

<h3>The sub and gsub method</h3>
Ruby has a handy string method to replace part of a string.
```ruby
message = 'The sky is blue'
puts message.sub 'blue', 'red'
# The sky is red
```
Note the method call has no parentheses to enclose parameters. In Ruby those are optional.
```ruby
# Valid
puts message.sub 'blue', 'red'

# Also valid
puts message.sub('blue', 'red')
```
The convention is to omit the parentheses unless the code seems clearer with them.

The sub method replaces the first occurrence. The gsub method replaces all the occurrences.
```ruby
message = 'The sky is blue and the car is also blue'
puts message.gsub 'blue', 'red'
# The sky is red and the car is also red
```
<h3>Strip method</h3>
Remove white space before or after a string
```ruby
puts '  Welcome to Ruby World  '.strip
# 'Welcome to Ruby World'

```
<h3>Chaining methods</h3>
It is possible to chain string methods
```ruby
name = '   Mike Taylor '
puts name.sub('Mike', 'Paul').strip.downcase
# paul taylor
# In that specific situation, using the parentheses make the code easier to read
```

<h1>Exercise</h1>
Create a little program that:
- Input the user name and store the result in a variable
- Input the user password and store the result in a variable
- Remove password before or after white space
- Convert the password to lowercase
- Display user name and password but replace the password letter 'e' with a star  

Here are the result
```
Enter user name: _Mike
Enter user password: _secret

The user name is Mike and his password is s*cr*t
```

Solution
```ruby
print 'Enter user name: '
name = gets.chomp

print 'Enter user password: '
password = gets.chomp.strip.downcase

puts "The user name is #{name} and his password is #{password.gsub('e', '*')}"
```

<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>