Variables are utilized to store the data that referenced and manipulated in a Computer program. Which additionally give a method for marking information with a descriptive name, so these programs can be easily understood by all the users and useful to consider variables containers that hold data.
User can assign the variable by utilising the
= symbol. The name of the variable goes on the left and the value user need to store in the variable goes on the right which is shown below.
[ruby]
irb :001 > first_name = 'John'
=> "John"
[/ruby]
User can get the information by calling the
gets method in which gets stand for
get string. When using the gets method initially user need to type in information and then press the enter key which is shown below.
[ruby]
irb :001 > name = gets
john
=> "john\n"
[/ruby]
Variable Scope
A variable's scope figures out where in a snippet a variable is accessible for utilize. A variable's scope is characterized by where the variable is introduced or made. In Ruby, variable scope is characterized by a block which is known as a bit of code taking after a strategy conjuring, typically delimited by either curly braces {} or do/end. Know that not all do/end sets suggest a block which is shown below.
[ruby]
# scope.rb
a = 5 # variable is initialized in the outer scope
3.times do |n|
a = 3 # is a accessible here, in an inner scope?
end
puts a
[/ruby]
Ruby have the five types of variables these are listed below.
- Global Variables
- Instance Variables
- Class Variables
- Local Variables
Global Variables
Global Variables start with $ if the global variable value is nil those are known as Uninitialized Global Variables which create notices with - w choice. Global status can be changed by global variables. Which is not prescribed to utilize Global Variables which make obscure programs. The code below demonstrates the global variables.
[ruby]
$global_variable = 10
class Class1
def print_global
puts "Global variable in Class1 is #$global_variable"
end
end
class Class2
def print_global
puts "Global variable in Class2 is #$global_variable"
end
end
class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.
Instance Variables
Instance variables start with @. Uninitialized Instance variables the variables whose esteem value is nil those are known as Inatance variable and these are deliver notices with the - w alternative.
[ruby]
class Customer
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methods
cust1.display_details()
cust2.display_details()
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.
Class Variables
Class variables start with @@ and which can be initialized before they can be utilized as a strategy definitions. Initially error will be created by Referencing a uninitialized class variable. Class variables are characterised to share class or modules. overriding class variables deliver notices with the '- w' choice.
[ruby]
class Customer
@@no_of_customers=0
def initialize(id, name, addr)
@cust_id=id
@cust_name=name
@cust_addr=addr
@@no_of_customers += 1
end
def display_details()
puts "Customer id #@cust_id"
puts "Customer name #@cust_name"
puts "Customer address #@cust_addr"
end
def total_no_of_customers()
puts "Total number of customers: #@@no_of_customers"
end
end
# Create Objects
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
# Call Methods
cust1.total_no_of_customers()
cust2.total_no_of_customers()
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.
Local Variables
Local variables represented with a lowercase letter starting letter or _. The local variable scope will be from class, def, module, or do to the corresponding end or when can be access from a block's opening brace to closing brace {}.
Ruby Constant begins with an upper case letter in which constants are defined with a class or which can be accesse with in the class and out of side class can be accessed globally which shown in below code.
[ruby]
class Example
VAR1 = 100
VAR2 = 200
def show
puts "Value of first Constant is #{VAR1}"
puts "Value of second Constant is #{VAR2}"
end
end
# Create Objects
object=Example.new()
object.show
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.