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
Integerto create new types of objects. - To see which class a particular object is, you can use the
classmethod. - Objects have instance methods, or methods that can be called on a specific instance of a class, like
reversefor Strings. - To see which instance methods are available for a given object, you can use the
methodsmethod. - 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_accessorare 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的一些依赖库还没 ...
随机推荐
- 转摘http://blog.csdn.net/hulihui/article/details/3351922#s6
译文:构建DataGridView的定制NumericUpDown单元格(Cell)和表格列(Column) 分类: DataGridView控件 2008-11-22 20:58 3555人阅读 评 ...
- [AaronYang]C#人爱学不学[5]
这世上有三样东西是别人抢不走的:一是吃进胃里的食物,二是藏在心中的梦想,三是读进大脑的书 --Aaronyang的博客(www.ayjs.net) 1. 数组-的疑惑? 1.1 多维数组 ...
- Nodejs学习笔记(五)--- Express安装入门与模版引擎ejs
目录 前言 Express简介和安装 运行第一个基于express框架的Web 模版引擎 ejs express项目结构 express项目分析 app.set(name,value) app.use ...
- AngularJS - 快速入门
刚开始接触时总是去wiki或各种百科以了解一番. 它们会告诉我一些MVVM.双向数据绑定.依赖注入等等名词,觉得这些名词好上档次,然后我很可能就不打算用这个东西了. AngularJS是什么? 完全使 ...
- Sublime Text 3 破解版 + 注册机 + 汉化包 + 教程
SublimeText 是一个代码编辑器,也是HTML和散文先进的文本编辑器. SublimeText 是由程序员 Jon Skinner 于2008年1月份所开发出来,它最初被设计为一个具有丰富扩展 ...
- js回掉页面后台代码-简单demo
后台代码: public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler { protected void Pag ...
- 改Bug
一:新闻查询失败 1.velocity:R对象里的变量不区分大小写? 哦,应该是的! 2.表单的button是默认就有提交功能的哦! 3.velocity变量在页面上的解析: 为什么会出错呢? 难 ...
- 【CodeForces 620D】Professor GukiZ and Two Arrays
题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...
- CoreOS Architecture Learning
目录 . CoreOS简介 . CoreOS部署.安装.使用 . CoreOS命令使用 1. CoreOS简介 0x1: CoreOS和Docker的关系 我们先来看一张Docker的架构图
- The prefix "mvc" for element "mvc:annotation-driven" is not bound 的解决方法
添加 xmlns:mvc="http://www.springframework.org/schema/mvc" http://www.springframework.org/sc ...
- Adding Cache-Control headers to Static Files in ASP.NET Core