Fundamentals of Object-Oriented Programming Applied in Ruby
Fundamentals of Object-Oriented Programming Applied in Ruby
applied in Ruby
Cole Vohs
1 Introduction
1
2 Ruby Evaluation
2
3 Object-Oriented Programming (OOP)
OOP is a paradigm in which objects are instantiated and manipulated throughout a program.
Objects are instances of classes with data fields, called attributes, and specific behaviors, called
methods. It has four core concepts: Abstraction, Polymorphism, Encapsulation, and Inheritance.
Abstraction is the concept of hiding all implementation details to the user, and only providing
the documentation required to work with the interface. This allows the user to implement their
own complex logic on top of it without worrying about the underlying complexity.
Polymorphism refers to the ability of a variable to be bound to entities of multiple types. This
means sending the the same message two two differently typed variables will produce different
results [9]. Encapsulation is a form of information hiding. It hides implementation details from
the other objects. The most simple form of this is creating what are called ”getter and setter”
methods to gain indirect access to a class’s attributes. Giving a user direct access to attributes
can lead to unreliability very easily, so giving them indirect access makes sure they only do so
in the intended way. Inheritance is the final key concept of OOP, and it involves creating classes
that gain attributes and methods of another class. The created classes are called derived classes
and they often serve to extend the functionality of the parent class for a more specialized
purpose [5]. These four concepts represent OOP, and they come with a few advantages and
disadvantages.
3.1 Advantages
The largest advantage OOP provides is productivity, and in a variety of ways too. It’s modular,
making all tasks able to be separated from each other, as well as extensible, making tasks able to
be built on top of each other. This all ties into code reusability, allowing code to be reused via
inheritance and the like throughout different aspects in a program. This is an improvement over
traditional procedural languages. Reusability leads to many other benefits, such as faster and
lower cost development. Modularity makes the code easier to maintain. If all processes are
3
separated, one section breaking is not catastrophic, and can be patched quickly to work with the
rest of the systems. Reusability, Extensibility, and Modularity all lead to higher quality software
because increased development productivity allows more effort to be put towards debugging [1].
3.2 Disadvantages
The disadvantages in OOP come in a few forms. One is the intuitiveness of problem solving. In
imperative languages, the emphasis is all in the algorithm, and in how that algorithm is solving
the problem. In OOP, programmers create and manipulate the objects used to solve the
problem. Getting these objects to interact with each other can be less intuitive for someone
adopting an object-oriented language, and it’s generally more lines of code compared to
imperative programming. The other major concern when deciding to go with OOP or not is
performance. Object-oriented programs are generally larger and require more instructions to
run than procedural programs. This makes OOP less practical for programs that need every inch
of efficiency they can get [1] [4].
Classes in Ruby can be easily defined by using the keyword class. This short code block shows
the anatomy of a class:
class Simple
def initialize data_one
@data_one = data_one
end
def get_data
4
@data_one
end
end
This class Simple has only one data member, denoted by an @ symbol in Ruby, called
@data one, and it can only be initialized via the initialize method, which serves as the
constructor for this class. Encapsulation already plays a role here in this class, and in every
Ruby class, because attributes are accessible only via methods. The method to get the @data one
attribute is called get data, and simply returns the attribute. Under the class definition, an
object called foo is created with the parameter "simple class", which will instantiate the
@data one attribute to that string. The last line will call a ”getter” method for the attribute,
which is mandatory if access to this attribute is needed [5] [7].
Inheritance and Polymorphism are at the forefront of Ruby classes, and they often go
hand-in-hand. They’re the whole basis for code reusability and why the development process of
OOP is faster. The following class is an example of an inherited class in Ruby followed by some
statements to show its behavior:
5
def get_data
@data_two
end
end
First, note that in the class declaration the < operator is used to designate what the class
Inherit inherits from, which is the Simple class described earlier. Inside the constructor, the
super function is called, which calls the constructor of the parent class. From there, new,
non-inherited data members can be initialized. Notice that the get data method is redefined
here to get the @data two attribute. If this new definition were to be omitted, calling get data
would return @data one, because that’s what it’s parent class does. This is an example that
shows off polymorphism. The same message (or method call) get data is being sent to two
objects, yet is is yielding different results due to the objects’ different types. Another way to
achieve polymorphism in Ruby is through the use of a module. A module is like a class,
meaning it has attributes and behaviors, but it can’t be instantiated to an object. Other classes
the user builds can inherit the module, giving it the behaviors of the module. Modules are
inherited in other classes with the include function (generally referred to as a ”mixin”)[2].
Ruby’s clear and intuitive method of inheritance make it easy to test class behaviors quickly and
in different ways.
6
4.3 Efficiency of Ruby
Ruby is slow for what many users use it for, but it shines in other areas. While Ruby itself is
general purpose, it’s most commonly used via the web application framework Ruby on Rails.
Rails, once a very strong and widely-used framework, has fallen off recently simply due to the
highly competitive nature of the web development scene. Rails is old and has had a lot of
features added, making it very flexible, and, ”Feature-rich frameworks like Rails have a lot of
code, and execute a lot more on each request because they are doing more stuff” [4]. Ruby takes
more computing resources, has larger performance issues, and is inapt for large scale
development. In terms of benchmarks, it is true that Ruby is slow, but only slightly more so
than it’s counterparts (notably Node.js)[3]. Ruby excels in flexibility and development process
efficiency. If rapid prototyping and fast development are important to a project, then Ruby is an
exceptional choice. This makes Ruby more practical, and perhaps one of the best choices, for
small scale OOP projects.
5 Conclusion
Overall, Ruby is a versatile and powerful OOP language that puts the programmer in the
spotlight. While it might be costly to perform large computations for an extended period of
time, it’s one of the best at being issued instructions quickly because of the smooth interface
Matsumoto strives for. The future of this language highly depends on when Ruby 3.0 (or Ruby
3x3) is being released, and whether or not it lives up to the promise of being three times faster
than Ruby 2.0. In the mean time, the quick development process still makes Ruby a good choice
for any project that can take advantage of it, or perhaps a first ”branch out” language for
newcomers to OOP. The combination of the three major principles of design applied to a fully
objective environment makes for a very interesting and unique language, and one that is apt for
analyzing OOP.
7
References
[3] Nodejs vs ruby on rails comparison 2017. which is the best for web development?, Feb 2017.
[6] Lerner, R. M. At the forge: Getting started with ruby. Linux J. 2005, 137 (Sept. 2005), 10–.
[7] Lerner, R. M. Introduction to ruby. Linux J. 2006, 147 (July 2006), 2–.
[9] Milojković, N., Caracciolo, A., Lungu, M. F., Nierstrasz, O., Röthlisberger, D., and
Robbes, R. Polymorphism in the spotlight: Studying its prevalence in java and smalltalk.
In Proceedings of the 2015 IEEE 23rd International Conference on Program Comprehension
(Piscataway, NJ, USA, 2015), ICPC ’15, IEEE Press, pp. 186–195.
8
Counts of words
File: paper.tex
Encoding: ascii
Sum count: 1848
Words in text: 1818
Words in headers: 30
Words outside text (captions, etc.): 0
Number of headers: 11
Number of floats/tables/figures: 0
Number of math inlines: 0
Number of math displayed: 0
Subcounts:
text+headers+captions (#headers/#floats/#inlines/#displayed)
0+7+0 (1/0/0/0) _top_
177+1+0 (1/0/0/0) Section: Introduction
344+2+0 (1/0/0/0) Section: Ruby Evaluation
239+3+0 (1/0/0/0) Section: Object-Oriented Programming (OOP)
137+1+0 (1/0/0/0) Subsection: Advantages
121+1+0 (1/0/0/0) Subsection: Disadvantages
0+3+0 (1/0/0/0) Section: Ruby and OOP
170+5+0 (1/0/0/0) Subsection: Classes and Objects in Ruby
305+3+0 (1/0/0/0) Subsection: Inheritance and Polymorphism
176+3+0 (1/0/0/0) Subsection: Efficiency of Ruby
149+1+0 (1/0/0/0) Section: Conclusion