Encapsulation and requiring Files

Source: Internet
Author: User

By encapsulating all the logic for an object, whether it's a Dog or a or a, you're User IceCreamShop able to keep all of the Logic and responsibilities of your object within its own scope. This lets avoid has to worry on other objects using methods that they shouldn ' t has access to.

Let's take one last look at how your Dog methods took advantage of encapsulation. Let's say also had some methods for your pet parrot in there as well. Here's what your code might has 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 Methodend

And here's what it would look like using OOP:

classDogdefDrink_water_from_bowl#code for methodEnddefWants_to_play? #code for methodEnddefWag_tail#code for methodEndEndclassParrotdefFly_away#code for methodEndEnd

Not only is it much more apparent which actions was supposed to being performed by which objects and now you won ' t has to W Orry about accidentally calling on fly_away your dog. Who knows what could has happened there!

To build off the benefits of encapsulation, OOP helps keep your code readable. By keeping all of the 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 if it comes to classes. As a rule of thumb, each class should exist in its own file. This is the if you ' re working on a application that have multiple classes, like ClothingStore and Customer , you know exactly where to Look-to-make changes to your code.

These files should has 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 Var Iable ( clothing_store.rb ).

But how does the get those files to interact with all other? Files don ' t just magically know that there is other files the need to being 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 and the last file, you can use require_relative . Let's see this in action.

# app.rb  "clothing_store.rb" "customer.rb"  # The rest of your code ...

This code would look for the files you specify relative to the current file. That means if customer.rb is one directory level above the current directory, and you ' d write require_relative “../customer.rb" .

Additionally, Ruby allows you to omit the .rb file extension by default. So your app.rb file can written instead as:

# app.rb  " clothing_store " "Customer" # The rest of your code ...

  • Nearly everything in Ruby are an object!
  • You can define your own classes in addition to the standard Ruby classes like to Integer create new types of objects.
  • To see which class A particular object are, you can use the class method.
  • Objects has instance methods, or methods that can is called on a specific instance of a class, like for reverse Strings.
  • To see which instance methods is 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 is scoped to a particular class, and always start with a @ .
  • attr_reader, attr_writer and is attr_accessor handy shortcuts for getter and setter methods.
  • Ruby lets you write some things in + convenient ways, like leaving off parentheses. This issyntactic sugar??.
  • You can use the initialize initial set-up for your classes.
  • You can split up your Ruby files using require_relative .
  • Generally speaking, each of the class should belong in its own file.

Encapsulation and requiring Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.