Ruby is a perfect object-oriented programming language. Features of an object-oriented programming language include:
Data encapsulation
Data abstraction
Polymorphisms
Inherit
These features will be discussed in object-oriented Ruby.
An object-oriented program that involves classes and objects. A class is a blueprint created by individual objects. In object-oriented terms, your bike is an instance of the bicycle class.
Take a vehicle as an example, it includes wheels, horsepower (horsepower), fuel or fuel or gas tank capacity. These properties form the data members of the vehicle class. With these attributes, you can distinguish one vehicle from another.
Vehicles can also contain specific functions, such as halting, driving, and speeding. These functions form the data members of the vehicle class. Therefore, you can define a class as a combination of properties and functions.
The class Vehicle is defined as follows: By assigning different values to these data members, you can create classes In order to use Ruby for object-oriented programming, you need to first learn how to create objects and classes in Ruby. In Ruby, classes always use keywords You can use keywords Ruby provides four types of variables: Local variables: local variables are variables defined in the method. Local variables are not available outside the method. You will see more details about the approach in subsequent chapters. Local variables are in lowercase letters or Instance variables: instance variables can be used across methods in any particular instance or object. This means that instance variables can be changed from object to object. The instance variable places the symbol before the variable name Class variables: class variables can be used across different objects. The class variable belongs to the class and is an attribute of the class. Class variables place symbols before the variable name ( Global variables: class variables cannot be used across classes. If you want to have a variable that can be used across classes, you need to define global variables. Global variables are always marked with the dollar sign Use class variables Object is an instance of a class. Now you will learn how to create objects of a class in Ruby. In Ruby, you can use the methods of a class Method The following example creates a class Here, You can give the method When you want to declare a The following example creates a In this example, you can declare that you have In You can now create an object, as follows: In Ruby, functions are called methods. Each method in the class is defined by a keyword Method names always start with a lowercase letter. In Ruby, you can use keywords The following example defines a Ruby method: Here, The following example creates a class This will produce the following results: 6.11.1. Example #
ClassVehicle{Numberno_of_wheelsNumberhorsepowerCharacterstype_of_tankNumberCapacityFunctionspeeding{
}Functiondriving{ }Functionhalting{ } }
Vehicle
different examples of. For example, an airplane has threewheels, horsepower
1,000
the capacity of the fuel tank is
100
L. In the same way, a car has four wheels, horsepower
200
the capacity of the gas tank is
25
L.Define classes in Ruby #
class
start, followed by the name of the class. The first letter of the class name should be capitalized. Class
Customer
as follows:classCustomerend
end
terminates a class. All data members in a class are between the class definition and the
end
between keywords.Variables in the Ruby class #
_
start.
@
).
@@
).
$
) begin. 6.11.2. Example #
@@no_of_customers
can determine the number of objects being created, which determines the number of customers 6.11.3. Example #
classCustomer@@no_of_customers=0end
Use in Ruby new Method to create an object #
new
create an object.
new
is a unique approach that is predefined in the Ruby library.``new`` method belongs to a class method.
Customer
two objects of
cust1
and
cust2
:cust1=Customer.newcust2=Customer.new
cust1
And
cust2
is the name of two objects. The object name is followed by the equal sign (=), followed by the class name, followed by the dot operator and keyword
new
.Custom methods to create Ruby objects #
new
pass parameters that can be used to initialize class variables.
new
method, you need to declare the method while creating the class
initialize
.
initialize
method is a special type of method that will call the
new
method is executed.
initialize
methods:Example #
classCustomer@@no_of_customers=0definitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrendend
id
、
name
、
addr
as a local variable
initialize
Method. Here,
def
and
end
used to define Ruby methods
initialize
. In later chapters, youwill learn more about the method.
initialize
method, pass the values of these local variables to the instance variables
@cust_id
、
@cust_name
and
@cust_addr
. In this case, the value of the local variable is with the
new
method to pass on.cust1=Customer.new("1","John","Wisdom Apartments,
Ludhiya")cust2=Customer.new("2","Poul","New Empire road, Khandala")
Member functions in the Ruby class #
def
start, followed by the method name.
end
to end a method.classSampledeffunctionstatement1statement2endend
statement
1
and
statement
2
are methods within the class Sample
function
. The main body of the component. These statements can beany valid Ruby statement. For example, we can use the method
puts
to output
Hello
Ruby
, as follows:classSampledefhelloputs"Hello Ruby!"endend
Sample
and call the
hello
methods:#!/usr/bin/rubyclassSampledefhelloputs"Hello
Ruby!"endend#Use the above class to create objectsobject=Sample.newobject.hello
Hello Ruby!