A defining feature of the ruby programming language, object orientation bridges the gap between dynamic code and real world scenarios we want our applications to imitate in order to bring our end users an easy and almost intuitive experience. Websites have long since replaced most personal travel agents, providing instant access to vast amount of deals previously seen only by industry specialists. As more lifestyles and careers become digitized, object orientation becomes increasingly important in drawing more users to apps from various walks of life to our unique applications.
This blog entry will cover the introductory basics of object orientation in ruby, as such readers who’ve stumbled across this page should already be familiar with the syntax and functions of ruby data types from simple strings to complex hashes, as well as variables, methods, and return values. Object orientation makes heavy use of these objects as well as implements its own version of them, as you’ll (hopefully) see below.
Object orientation mostly revolves around the
class keyword, which you can consider a sort of advanced
method. Unless directed otherwise, variables and methods in object orientation will reside within the scope of the class they are created in. Whatever the class name is, the first letter must be capitalize and observe CamelCase instead of the typical snake_case we’ve seen before.
class Tesla#your code hereend
With that established, we can move onto the
instance objects. We name an instance we created from the
class through a new concept called
instantiation. It’s a lot simpler than it sounds. To instantiate something means to bring it into existence, to create it. We’re going to instantiate an object called
model3 from the Tesla class. The
.new you see is simply the code telling the class to create the new instance. Ruby also displays the return value of an instance once it is instantiated, the syntax contains the name of the Class the instance it was created from and the unique object identifier
object_id. The attached syntax means that two or more instances will never be mistaken for one another, even if they come from the same Class.
class Tesla#your code hereendmodel3 = Tesla.new#<Tesla:0x000055b1e0d41df8>
We know methods can be called on objects if that select object responds to that select method. You cannot call
.chomp on an array object, just like you cannot call
.pop on a string object. With object orientation, you can create your own methods to call on your instants.
class Tesla#your code hereend model3 = Tesla.newmodel3.methods =>
[:to_json, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :instance_variable_get, :public_methods, :instance_variables, :method, :public_method, :singleton_method, :define_singleton_method, :public_send, :extend, :to_enum, :enum_for, :pp, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :to_s, :display, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :yield_self, :taint, :untaint, :tainted?, :untrusted?, :untrust, :frozen?, :trust, :singleton_methods, :methods, :private_methods, :protected_methods, :!, :equal?, :instance_eval, :==, :instance_exec, :!=, :__id__, :__send__]
A simple method we can call on model3 would be
.class to see what class model3 belongs to (Tesla). Tesla doesn’t really have any methods built into it, so let’s create the first one;
class Tesla def ignition
puts "The Tesla is now turned on"
endendmodel3 = Tesla.new
model3.ignitionThe Tesla is now turned on
As instance methods give instance objects their features or behaviors, instance variables give them attributes or properties, and ‘teach’ them that this data defines them. While normal variables can really only be called in a method if they are passed in as an argument, instance variables have the added ability to be accessed by instance methods of a particular class using object orientation, defying the conventional limitations of the local scope.
class Tesla def name=(car_name)
@this_cars_name = car_name
endendmodel3 = Tesla.new
model3.name = "Optimus"
As you can see this requires creating two different but linked instance methods, the first being
name= followed by
name. The first method takes in car_name as an argument and then sets that argument as equal to the variable called this_cars_name. The second method has the singular responsibility of reading the name. This two-step method pairing creates a gateway from the inside of the Tesla class to the rest of the program code outside the class. However, none of this is possible without the
@ symbol; the visual indicator and proper syntax for an instance variable. This let’s the instance variable be accessible anywhere.
There is much more to object oriented programming in ruby, and the steps immediately following these enter deeper levels of abstraction in order to create more robust and reusable code, but if I add anymore to this I’d probably be charged with leaking classified information. Hope this helped.