Ruby Variables Beginner's Guide
Ruby Variables Beginner's Guide
We'll cover...
Let's do this :)
https://www.rubyguides.com/ruby-tutorial/variables/ 2/7
03/04/2024, 12:11 Ruby Variables: A Beginner's Guide
Example:
1. age = 32
Now when you type age Ruby will translate that into 32 .
Try it!
https://www.rubyguides.com/ruby-tutorial/variables/ 3/7
03/04/2024, 12:11 Ruby Variables: A Beginner's Guide
1. age * 10
2. # 320
1. age = 32
2. multiplier = 10
3.
4. age * multiplier
Important:
If you’re running this code from a file, instead of irb, then you should
use a method like puts to see the value of the variable.
Example:
1. puts total
2. # 320
https://www.rubyguides.com/ruby-tutorial/variables/ 4/7
03/04/2024, 12:11 Ruby Variables: A Beginner's Guide
constant ( APPLE )
You don’t need to worry too much about these right now, but it’s
good to know they exist.
This is only going to matter when you start learning about Object-
Oriented Programming, we’ll revisit this topic by then.
https://www.rubyguides.com/ruby-tutorial/variables/ 5/7
03/04/2024, 12:11 Ruby Variables: A Beginner's Guide
Practice Time!
Practice working with variables so you can develop an understanding
of how they work.
Now:
If you don’t use variables you would have to repeat every single
calculation every time that you want to refer to it.
And you wouldn’t have a way to name things so you know what
they are.
3. Multiply the value of orange & apple , then assign the result to a
new variable total .
When you feel comfortable using variables then go to the next lesson.
And if you haven’t installed Ruby yet make sure to check the first
lesson of this Ruby tutorial for beginners.
https://www.rubyguides.com/ruby-tutorial/variables/ 6/7