Sie sind auf Seite 1von 9

Fundamentals of Object-Oriented Programming

applied in Ruby

Cole Vohs

February 12, 2019

1 Introduction

Object-Oriented programming (OOP) is a very significant development in Computer Science. So


much so that out of the most widely-used languages today, many of them support OOP in some
form, large or small. However, some language developers go all-in when choosing the path of
OOP, and one recent language that does that is Ruby. Ruby was developed by Yukihiro ”Matz”
Matsumoto in 1995. The tag line of Ruby is simple: It’s an ”Object-Oriented Scripting Language.”
What this doesn’t tell you is that in Ruby everything is an object, even primitive data types,
making it purely object-oriented [10]. Reuven M. Lerner in his At the Forge: Getting Started with
Ruby describes it as a, ”Cross between Perl and Smalltalk,” two other object-oriented languages
created in the 1980s [6]. With work currently being done on Ruby 3.0, which Matsumoto wants
to be three times faster than Ruby 2.0, it might be worth taking another look at this language.
Ruby’s fully object-oriented environment forms a powerful and versatile language, and core
concepts of OOP can easily be related to the language’s design.

1
2 Ruby Evaluation

Ruby is high-level by nature since everything can be manipulated as an object. High-level


languages often feature good readability and writability because the syntax comes somewhat
naturally to the user or reader, but Ruby’s readability is an interesting case. Matsumoto uses
core concepts of user-interface design in Ruby’s design, which are conciseness, consistency, and
flexibility [8]. This makes reading code without or with poor documentation easier to follow
along with, especially for OOP veterans. In fact, since Ruby originated in Japan, it had a, ”Lack
of documentation,” until it got to other places of the world in the early 2000s via Programming
in Ruby by Dave Thomas, or what Ruby programmers often call ”The Pickaxe Book” [6].
Another aspect of Ruby that makes it cleaner to read is how blocks of code are structured. They
all begin with their respective keywords and end with the keyword end, rather than with a }
like in many C-like languages (which is sometimes optional, while the end keyword is
consistent). Having clear block definitions like this make them stand out easily to the reader,
making separating blocks visually easier on the eyes. Ruby’s readability loses points due to the
fact that it is dynamically typed, and variable types don’t have to be specified the same way
they do in a language like Java or C++. While this is a small benefit for the person writing the
program, the readability suffers greatly for people maintaining the code. This dent in readability
doesn’t impact its reliability though. Since Ruby is strongly typed, type errors always cause
Ruby to throw an exception. Ruby’s cost is also an interesting case. In terms of the cost of the
development process itself, it’s fairly low. This is because of the three main design principles
mentioned earlier. Code can be quickly and easily written and reused for many aspects of a
project, making the development process much more streamlined. Overall, Ruby is very
readable, writable, reliable, and generally not costly (in terms of development, but language
efficiency will be covered later), making it a rather well-rounded language.

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].

4 Ruby and OOP

4.1 Classes and Objects in Ruby

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

foo = Simple.new "simple class"


#returns "simple class"
puts foo.get_data

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].

4.2 Inheritance and Polymorphism

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:

class Inherit < Simple


def initialize data_one, data_two
super data_one
@data_two = data_two
end

5
def get_data
@data_two
end
end

foo = Simple.new "simple class"


# returns "simple class"
puts foo.get_data

bar = Inherit.new "simple class", "inherited class"


# returns "inherited class"
puts bar.get_data

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

[1] Advantages and disadvantages of object-oriented programming (oop).

[2] The object model.

[3] Nodejs vs ruby on rails comparison 2017. which is the best for web development?, Feb 2017.

[4] Berkopec, N. Is ruby too slow for web-scale?

[5] Bodnar, J. Object-oriented programming in ruby.

[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–.

[8] Matsumoto, Y. The ruby programming language, Jun 2000.

[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.

[10] , T. Ruby. Linux J. 2002, 95 (Mar. 2002), 4–.

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

Das könnte Ihnen auch gefallen