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
Encapsulation and Requiring Files的更多相关文章
- Adding Cache-Control headers to Static Files in ASP.NET Core
Thanks to the ASP.NET Core middleware pipeline, it is relatively simple to add additional HTTP heade ...
- Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...
- The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files
看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...
- 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1
在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...
- Find and delete duplicate files
作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file name" ...
- Android Duplicate files copied in APK
今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...
- Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...
- files list file for package 'xxx' is missing final newline
#!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...
- Mac osx 安装PIL出现Some externally hosted files were ignored (use --allow-external PIL to allow).
出现这个问题Some externally hosted files were ignored (use --allow-external PIL to allow)的主要原因是PIL的一些依赖库还没 ...
随机推荐
- 每天一个linux命令(53):wget命令
Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ...
- 每天一个linux命令(47):traceroute命令
通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一 ...
- reboot-css
dd, label { margin-bottom: .5rem; }abbr[title] { text-decoration: none; }abbr[title] { border-bottom ...
- 小结-Splay
参照陈竞潇学长的模板写的BZOJ 3188: #include<cstdio> #include<cstring> #include<algorithm> #def ...
- C#中File类的文件操作方法详解
File类,是一个静态类,主要是来提供一些函数库用的.静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件.File类方法的参量很多时候都是路径path.F ...
- 【CodeForces 472A】Design Tutorial: Learn from Math
题 题意:给你一个大于等于12的数,要你用两个合数表示出来.//合数指自然数中除了能被1和本身整除外,还能被其他的数整除(不包括0)的数. 分析:我们知道偶数除了2都是合数,给你一个偶数,你减去一个偶 ...
- Ubuntu安装VMware Tools的方法
最后我将提供一个12版本的VMware Tools集合,包括了linux.iso. 背景: VMware Tools是VMware虚拟机中自带的一种增强工具,相当于VirtualBox中的增强功能(S ...
- (转)eclipse项目导入到android studio中
原文:http://www.cnblogs.com/lao-liang/p/5016541.html?utm_source=tuicool&utm_medium=referral Androi ...
- cheerio, dom操作模块
cheerio 为服务器特别定制的,快速.灵活.实施的jQuery核心实现. Introduction 将HTML告诉你的服务器 var cheerio = require('cheerio'), $ ...
- 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Adding Cache-Control headers to Static Files in ASP.NET Core