The Wayback Machine - https://web.archive.org/web/20210424152247/https://dev.to/ericchapman/learn-ruby-101-variables-puts-and-gets-3k63

DEV Community

loading...
Cover image for Learn Ruby 101: Variables, puts and gets

Learn Ruby 101: Variables, puts and gets

ericchapman profile image Eric Chapman ・3 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.

You want to learn Ruby or your Ruby is a bit rusty?

In this series we will start from the beginning and will learn 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.

Run your Ruby code

No need to go through a complete install. Just go to this web site 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...

Ruby Variables

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 use later on.

For example let say you want to store the user name you can use a variable call name and set is value to Mike Taylor.

name = 'Mike Taylor'
Enter fullscreen mode Exit fullscreen mode

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 specified 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 type and how to create them

# string
full_name = 'Mike Taylor'

# integer number
count = 20

# float number
book_price = 15.80

# booleans
active? = true
admin_user? = false
Enter fullscreen mode Exit fullscreen mode

Ruby also have more advance variables type like array, hash, structure and class. We will cover all of those in details later.

Output

In Ruby it is possible to output information to the console/terminal.

For example let's send our name variable to the console

name = 'Mike Taylor'
puts name
Enter fullscreen mode Exit fullscreen mode

The puts method will take any value we give him and print it to the console...

Mike Taylor
Enter fullscreen mode Exit fullscreen mode

Others example

name = 'Mike Taylor'
puts 'Hello World'
puts 'Hello', name
Enter fullscreen mode Exit fullscreen mode
Hello World
Hello
Mike Taylor
Enter fullscreen mode Exit fullscreen mode

As you can see we can send multiple value 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:

name = 'Mike Taylor'
print 'Hello ', name
Enter fullscreen mode Exit fullscreen mode
Hello Mike Taylor
Enter fullscreen mode Exit fullscreen mode

Input

How about getting info from user. In Ruby we use the method gets to do just that

print 'Enter user name: '
name = gets
Enter fullscreen mode Exit fullscreen mode

The console will then wait for user input:

Enter user name: _
Enter fullscreen mode Exit fullscreen mode

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

print 'Enter user name: '
name = gets.chomp
Enter fullscreen mode Exit fullscreen mode

Exercice

Create a little program that ask for user name and user age and save the result in name and age variable.

Then display name and age variable in the console

Solution

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
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 (0)

pic
Editor guide