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的一些依赖库还没 ...
随机推荐
- [BZOJ1406][AHOI2007]密码箱(数论)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1406 分析: (x+1)(x-1)是n的倍数 于是可以把n分解成n=ab,则a为(x+ ...
- Scala 中的函数式编程基础(三)
主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...
- RestFul API初识
python Restful API 微博开放平台: open.weibo.com: 点击文档进入API查看界面 点击API文档进行查看: 比如点开粉丝数的API可以看到: pro.jsonlint. ...
- Java线程池应用
Executors工具类用于创建Java线程池和定时器. newFixedThreadPool:创建一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程.在任意点,在大多数 nThread ...
- nginx配置反向代理示例
环境: nginx1:192.168.68.41 tomcat1:192.168.68.43 tomcat2:192.168.68.45 nginx安装网上很多教程,我是用yum安装的. 配置ngin ...
- Java 并发-访问量
有几个常用的措施 1.对常用功能建立缓存模块 .尽量使用缓存,包括用户缓存,信息缓存等,多花点内存来做缓存,可以大量减少与数据库的交互,提高性能.统计的功能尽量做缓存,或按每天一统计或定时统计相关报表 ...
- BZOJ-1192 鬼谷子的钱袋 2^n有关数论
1192: [HNOI2006]鬼谷子的钱袋 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2473 Solved: 1806 [Submit][St ...
- 移动H5页面,keyup事件不好使用处理解决
1.mouse事件换成touch,pointer事件,keyup换成textInput事件
- Intrusion Analysis Learning
目录 . 入侵分析简介 . 基于日志的入侵分析技术 . 入侵分析CASE . 入侵分析CASE . 入侵分析CASE . 入侵分析CASE 1. 入侵分析简介 Windows 清除日志的方法 wmic ...
- Linux Rootkit Learning
目录 . 学习Rootkit需要了解的基础知识 . 挂钩(HOOKING) . 直接内核对象操作 . LSM框架(Linux Security Module)于LKM安全 . rootkit检测技术及 ...
- Adding Cache-Control headers to Static Files in ASP.NET Core