Ruby Programming Language
Ruby Objects
By Mark Ciotola
First published on August 23, 2019. Last updated on February 15, 2020.
While not necessary for simple simulations, the object-oriented capabilities of Ruby can make programs more modular, allow better modeling of complicated entities, and help avoid the need to rewrite code.
In orbject-oriented programming, objects represent entities, such as animals or companies. Objects belong to classes and subclasses. Subclasses inherit the characteristics of classes they are derived from, but also add their own particular characteristics to themselves.
For example, say you created a class called animal that could include characteristics such as number of legs. Then you created a subclass called dog, what could also have characteristics such as bark pitch. A dog object would inherit the characteristic such as legs. Characteristics such as number of legs or bark pitch involve data and are called properties. An object can have one or more attached methods, which are procedures that exhibit some sort of behavior.
An object is then an instance of a particular class. For example, an object named Rover would be an instance of the dog class.
Everything in Ruby is an object, and thus contains many attributes. Each variable you create is an object. When you add a “.to_s” to the end of your variable, you are really obtaining the string attribute of that object. Here are several useful attributes:
- “.to_f” returns the float attribute of the object.
- “.to_i” returns the integer attribute of the object.
- “.to_s” returns the string attribute of the object.
- “.length” returns the length of the object.
- “.inspect” returns information about the object
Source Code
The source code for the sample object-oriented Ruby program is shown below. This program illustrates the modular nature of object-oriented programs. It first creates a Regime class that applies to many different types of governments. Then the Dynasty subclass is derived from the Regime class, but adding additional descriptive information. A regime object sampleregime is created for the First French Republic, and then a dynasty object sampledynasty is created for the Romanov dynasty. The code snippet sampledynasty.title includes sampledynasty which is the name of the object and title which is the method being called.
The following simple program partly derived from [4] demonstrates Ruby’s object-oriented nature, and illustrates several object concepts.
# Create the Regime class class Regime def initialize(name) # Capitalize name of Regime @name= name.capitalize end def title # Create a method to output the regime name print @name end def description # Add a description to the regime name print @name + " is the name of a regime." puts end end # Create the Dynasty class as a subclass of Regime class Dynasty < Regime def description # Add a description to the dynasty name print " is the name of a regime that is also a dynasty." puts end end # Create new Regime and Dynasty object. sampleregime = Regime.new("First French Republic") sampledynasty = Dynasty.new("Romanov") # Output puts puts "OUTPUT FROM OBJECT DEMONSTRATION" puts "--------------------------------" sampleregime.description sampledynasty.title # title inherited from regime class sampledynasty.description puts # puts by itself adds an extra line of space
Results
The output displays the title of each created object, along with an appropriate description for that type of object.
OUTPUT FROM OBJECT DEMONSTRATION −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− First french republic is the name of a regime. Romanov is the name of a regime that is also a dynasty.
Reference
[4] Ruby-Lang.org, About. http://www.ruby-lang.org/en/about/, last viewed on 22 March 2013.