Sharpen your Ruby: Part 1
Tags: ruby, rails, beginners, tutorial
Published: 2021-08-22 13:47:49

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

<a href="https://twitter.com/EricTheCoder_?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">Follow me on Twitter @EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<hr>

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. 

<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 you are new and need help, you can comment below or send me a message.

<h2>Run your Ruby code</h2>
No need to go through a complete install. Just go to this website https://replit.com/languages/ruby and start learning right now. You will have plenty of time to figure out the Ruby installation on your local machine later on...

<h1>Ruby Variables</h1>
If you’re new to programming, variables are the fundamental building blocks of a programming language as they are used to store different values that you want to process in your code.

Once the variable is store in program memory, it can be used later on.

For example, let say you want to store the user name you can use a variable call name and set its value to Mike Taylor. 
```ruby
name = 'Mike Taylor'
```
In Ruby string is enclosed with quotation marks.

The variable name we just created is a string variable. In Ruby, we don't have to specify the variable type. 

Ruby is a Just-in-time (JIT) interpreted language. Which automatically recognizes the data type based on what variables are stored.

Here are some Ruby basic variables types and how to create them
```ruby
# string
full_name = 'Mike Taylor'

# integer number
count = 20

# float number
book_price = 15.80

# booleans
active? = true
admin_user? = false
```
Ruby also has more advanced variables types like an array, hash, structure, and class. We will cover all of those in detail later.

<h1>Output</h1>
In Ruby, it is possible to output information to the console/terminal.

For example, let's send our name variable to the console
```ruby
name = 'Mike Taylor'
puts name
```
The puts method will take any value we give him and print it to the console...
```
Mike Taylor
```

Others example
```ruby
name = 'Mike Taylor'
puts 'Hello World'
puts 'Hello', name
```
```
Hello World
Hello
Mike Taylor
```
As you can see we can send multiple values to puts method and he will display all of them.

Another Ruby method very similar to puts is the method print. Print can display something to the console but will not send the line break after each print. Example:
```ruby
name = 'Mike Taylor'
print 'Hello ', name
```
```
Hello Mike Taylor
```
<h1>Input</h1>
How about getting info from the user. In Ruby we use the method gets to do just that
```ruby
print 'Enter user name: '
name = gets
```
The console will then wait for user input:
```
Enter user name: _
```
The gets method will return everything you type plus a line break characters. If you don't want to read the line break characters use the chomp method to remove that last character
```ruby
print 'Enter user name: '
name = gets.chomp
```

<h2>Everything in Ruby is an object</h2>
The following concept will be a bit more advance. Beginners could have hard time to fully understand this concept. (and it is ok).

In Ruby, everything is an object. Event types like integer, string, array are all objects. All Ruby objects inherit properties and methods from their parent object. 

Here an example
```ruby
name = 'Mike'
puts name.upcase # MIKE
```
In this example, we use the method upcase to convert our string to upper case. How is that's possible? The name variable is a string and the string in Ruby is an object. All string objects have already many pre-build methods to help the developer.

The .upcase method is not the only method provided by the string object. There are many more methods available. We will discover some of those in future posts.

If you want to know all available methods for string or any other Ruby objects you can consult Ruby official documentation: https://www.ruby-lang.org/en/documentation/  

We will come back later to this concept, but just keep it in your mind: Everything in Ruby is an object.

<h1>Exercise</h1>
Create a little program that asks for the user name and age and save the result in the name and age variable. 

Then display name and age variable in the console

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

print 'Enter user age: '
age = gets.chomp

puts 'The user name is: ', name
puts 'The user age is: ', age
```

<h3>Conclusion</h3>
That's it for today. The journey just started, stay tuned for the next post very soon. 

If you have any comments or questions please do so here or send me a message on Twitter. 

Follow me: <a href="https://twitter.com/EricTheCoder_?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-show-count="false">@EricTheCoder_</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>