In this tutorial, we will learn about variables in Ruby. We will cover the basics of declaring and using variables, including naming conventions.
Variables in Ruby are used to store data values that can be manipulated during program execution. Ruby is a dynamically typed language, meaning the data type of a variable can change at runtime.
The syntax to declare variables in Ruby is:
variable_name = value
When naming variables in Ruby, follow these conventions:
In Ruby, we can store an integer value in a variable.
For example,
num
.=
.# Declare and initialize an integer variable
num = 10
# Print the value of the variable
puts "The value of num is: #{num}"
The value of num is: 10
In Ruby, we can store a string value in a variable.
For example,
name
.=
.# Declare and initialize a string variable
name = 'Arjun'
# Print the value of the variable
puts "The value of name is: #{name}"
The value of name is: Arjun
In Ruby, we can store a boolean value in a variable.
For example,
is_true
.true
to the variable.# Declare and initialize a boolean variable
is_true = true
# Print the value of the variable
puts "The value of is_true is: #{is_true}"
The value of is_true is: true