Encapsulation and Requiring Files
By encapsulating all the logic for an object, whether it’s a Dog
or a User
or an IceCreamShop
, you are able to keep all of the logic and responsibilities of your object within its own scope. This lets you avoid having to worry about other objects using methods that they shouldn’t have access to.
Let’s take one last look at how your Dog
methods took advantage of encapsulation. Let’s say you also had some methods for your pet parrot in there as well. Here’s what your code might have looked like:
def drink_water_from_bowl(dog) # code for method end def wants_to_play?(dog) # code for method end def fly_away(parrot) # code for method end def wag_tail(dog) # code for method end
And here’s what it would look like using OOP:
class Dog def drink_water_from_bowl # code for method end def wants_to_play? # code for method end def wag_tail # code for method end end class Parrot def fly_away # code for method end end
Not only is it much more apparent which actions are supposed to be performed by which objects, but now you won’t have to worry about accidentally calling fly_away
on your dog. Who knows what could have happened there!
To build off the benefits of encapsulation, OOP helps keep your code readable. By keeping all of its instance methods tabbed in one level deep, it becomes much simpler to skim through your files.
Requiring Files
To take this organization to the next level, let’s address one last convention when it comes to classes. As a rule of thumb, each class should exist in its own file. This way, when you’re working on an application that has multiple classes, like ClothingStore
and Customer
, you know exactly where to look to make changes to your code.
These files should have the same name as the class they contain, with a few slight changes. Instead of naming your files in upper camel case (ClothingStore.rb
), you should name them using snake case, like you would name a variable (clothing_store.rb
).
But how do you get those files to interact with each other? Files don’t just magically know that there are other files that need to be included in your project. You’ll need to explicitly require those files.
Let’s say you’ve got three files: clothing_store.rb
, customer.rb
, and app.rb
. To include the contents of the first two files in the last file, you can use require_relative
. Let’s see that in action.
# app.rb require_relative "clothing_store.rb" require_relative "customer.rb" # The rest of your code...
This code will look for the files you specify relative to the current file. That means that ifcustomer.rb
was one directory level above the current directory, you’d writerequire_relative “../customer.rb"
.
Additionally, Ruby allows you to omit the .rb
file extension by default. So your app.rb
file can be written instead as:
# app.rb require_relative "clothing_store" require_relative "customer" # The rest of your code...
- Nearly everything in Ruby is an object!
- You can define your own classes in addition to the standard Ruby classes like
Integer
to create new types of objects. - To see which class a particular object is, you can use the
class
method. - Objects have instance methods, or methods that can be called on a specific instance of a class, like
reverse
for Strings. - To see which instance methods are available for a given object, you can use the
methods
method. - Getter and setter methods can be used to assign and retrieve the values of instance variables.
- Speaking of, instance variables are scoped to a particular class, and always start with a
@
. attr_reader
,attr_writer
, andattr_accessor
are handy shortcuts for getter and setter methods.- Ruby lets you write some things in more convenient ways, like leaving off parentheses. This issyntactic sugar 🍭.
- You can use
initialize
to take care of any initial set-up for your classes. - You can split up your Ruby files using
require_relative
. - Generally speaking, each class should belong in its own file.