Software Developer
Ruby is an open-source object-oriented scripting language invented in the mid-90s by Yukihiro Matsumoto.
Unlike languages such as C and C++, a scripting language doesn’t talk directly to hardware. It’s written to a text file and then parsed by an interpreter and turned into code. These programs are generally procedural, meaning they are read from top to bottom.
Object-oriented languages, on the other hand, break out pieces of code into objects that can be created and used as needed. You can reuse these objects in other parts of the program or even other applications.
Variables in Ruby are the reminiscence location in which we keep the statistics, and these facts might be utilized by ruby developers whilst needed. In ruby, it helps 5 forms of statistics they may be worldwide variable begins with $, the global variable is to be had for all and its fee can be nil; by using the default, use global variables best if required otherwise keep away from the use of it, example variable starts with @ and having scope as much as particular times, elegance variable, neighborhood variables having scope up to elegance, module and def. And consistent variables start with upper case and can not reassign or alter.
Declaration and initialization for the Ruby variable can be done in the below formats.
Global variables start with dollar sign like.
$ruby_global_variable = 15
@ruby_instance_variable = 15
@@ruby_class_variables = 15
_ruby_local_variables = 15
CONSTANT1 =15
CONSTANT2 =20
Below are the types of Variables in Ruby:
Global variables are start with a dollar($) symbol and contain nil value by default. It is not advisable to use the global variable in all cases.
Example:
In the below example we have defined a variable $ruby_global and it can be accessed inside the two classes called RubyClass1 and RubyClass2. As both, the classes contain a function print_ruby_global which we are on instances of classes. Please follow the below example along with the screen of outputs.
Code:
$ruby_global_variable = 11
class RubyClass1
def print_ruby_global
puts "RubyClass1 global variable output is #$ruby_global_variable"
end
end
class RubyClass2
def print_ruby_global
puts "RubyClass2 global variable output is #$ruby_global_variable"
end
end
rubyclass1obj = RubyClass1.new
rubyclass1obj.print_ruby_global
rubyclass2obj = RubyClass2.new
rubyclass2obj.print_ruby_global
Instance variables start with @ symbol. Example for instance variables are given below.
Below example can be explained in the following steps:
class UserClass
def initialize(user_id, user_name, address)
@user_id = user_id
@user_name = user_name
@user_addr = address
end
def show_details()
puts "user id is #@user_id"
puts "name of the User is #@user_name"
puts "User address is #@user_addr"
end
end
#
user1 = UserClass.new("1", "Ranjan", "Mount View Apartment guindy, Chennai")
user2 = UserClass.new("2", "Ajay", "B-9 Dhanbad, Jharkhand")
user3 = UserClass.new("2", "Sujoy", "T nagar, Chennai")
user4 = UserClass.new("2", "Vijay", "New ashok nagar, Delhi")
# Call the Methods of class for displaying the details
user1.show_details()
user2.show_details()
user3.show_details
user4.show_details
Class variables can be defined with the @@ symbol. If someone will override the class variable then it will show a warning. Below example can be explained in the following steps:
Please follow the below example along with the output of the screen.
Code:
class User
@@no_of_users = 0
def initialize(u_id, u_name, u_address)
@user_id = u_id
@user_name = u_name
@user_addr = u_address
end
def show_details()
puts "User ID is #@user_id"
puts "User name is #@user_name"
puts "User Address is #@user_addr"
end
def total_users()
@@no_of_users += 1
puts "Count for the total number of users is: #@@no_of_users"
end
end
user1 = User.new("1", "Ranjan", "Mount View Apartment guindy, Chennai")
user2 = User.new("2", "Ajay", "B-9 Dhanbad, Jharkhand")
user3 =User.new("2", "Sujoy", "T nagar, Chennai")
user4 =User.new("2", "Vijay", "New ashok nagar, Delhi")
# Call the Methods of class for displaying the details
user1.total_users()
user2.total_users()
user3.total_users()
user4.total_users()
Constant variables start with the upper case letter. The constant variables can not reassign their values. Constants defined inside the class can be accessed inside the class and those defined inside the modules can be accessed inside the modules, we can also define the constant as the global which outside the class and module and will be available for all. An example of the constant variable is given below.
Explanation:
First, we have created a class with the name Example For Constant and this class contains two variables (CONSTANT1 and CONSTANT2).
We created a function inside the class Example For Constant with name display_constant and this function contains the logic to display both constant values.
Example:
Code:
class ExampleForConstant
CONSTANT1 = 101
CONSTANT2 = 201
def display_constant
puts "constant first value is #{CONSTANT1}"
puts "constant second value is #{CONSTANT2}"
end
end
# create an object and call the method display_constant.
object = ExampleForConstant.new()
object.display_constant