[Ruby] LEVEL 2 Methods and Classes
Optional Arguments
Set default arguments, when we don't need to call it, we can simply skip it.
def new_game(name, year=nil, system=nil)
{
name: name,
year: year,
system: system
}
end
game = new_game("Street Figher II")
Options Hash Argument
Sometimes, optinal argumetns also not that good. For exmaple, we have one argument added to the end, if we do want pass in reply_id and year, system we don't need to pass in, then we need to put placeholder in the function call.
def new_game(name, year=nil, system=nil, reply_id = nil)
{
name: name,
year: year,
system: system
}
end
game = new_game("Street Figher II", nil, nil, 50)
Therefore we can use options has argument:
def new_game(name, options={})
{
name: name,
year: options[:year],
system: options[:system]
}
end
game = new_game("Street Figher II",
year: 1992,
system: "SNES")
Exception
def get_tweet(list)
unless list.authorized?(@user)
raise AuthorizationException.new
end
list.tweets
end #raise an Exception instead begin
tweets = get_tweets(my_list)
rescue AuthorizationException
warn "You are not authorized to access this list"
end
[Ruby] LEVEL 2 Methods and Classes的更多相关文章
- HeadFIrst Ruby 第二章总结 methods and classes
HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...
- Abstract Methods and Classes
阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...
- ruby Methods, Procs, Lambdas, and Closures
define simple method定义简单方法 关键字def用于方法定义,在其后是方法名和可选的参数名列表,参数名列表会用一对圆括号括住.构成方法主体的代码放在参数列表之后,end用于结束方法定 ...
- Effective Java 13 Minimize the accessibility of classes and members
Information hiding is important for many reasons, most of which stem from the fact that it decouples ...
- Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses
5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...
- ruby中顶层定义的方法究竟放在哪里?
ruby中顶层(top level)中定义的方法放在main中,证明如下: self.private_methods(false) #IN TOP LEVEL 那么methods方法究竟是在哪定义的, ...
- Override is not allowed when implementing interface method Bytecode Version Overriding and Hiding Methods
java - @Override is not allowed when implementing interface method - Stack Overflow https://stackove ...
- TIJ——Chapter Seven:Reusing Classes
Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...
- Analysis about different methods for reading and writing file in Java language
referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...
随机推荐
- SWFUpload(转载)
网上的例子介绍的文档真的很多.下面简单介绍一下 SWFUpload的文件上传流程是这样的: 1.引入相应的js文件 2.实例化SWFUpload对象,传入一个配置参数对象进行各方面的配置. 3.点击S ...
- python - StringIO文本缓冲
参考:http://pymotwcn.readthedocs.org/en/latest/documents/StringIO.html 类StringIO提供了一个在内存中方便处理文本的类文件(读, ...
- html的input输入框提示信息 点击隐藏
<input type="text" <!-- 文本框 --> name="textfield" value="请输入...& ...
- Hash索引和BTree索引区别
(1)Hash 索引仅仅能满足"=","IN"和"<=>"查询,不能使用范围查询. 由于 Hash 索引比较的是进行 Hash ...
- Ubuntu 字体安装
命令安装: 以微软雅黑字体为例(其他的宋体.黑体等点阵字体都一样的),我们的雅黑字体文件是:Yahei.ttf(放在自己的主目录下)(在widows目录的Fonts目录下找需要的字体)由于我是双系 ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- c# 基础连接已经关闭: 连接被意外关闭,错误的解决
原文:c# 基础连接已经关闭: 连接被意外关闭,错误的解决 调试一个使用HttpWebRequest模拟提交表单的程序的时候频繁出现上述错误提示,google了一下发现了几个解决方案.1.在appli ...
- “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ”
草泥马的adb: “adb server is out of date. killing.... ADB server didn't ACK * failed to start daemon * ” ...
- ♫【jQuery】detach
Jquery empty() remove() detach() 方法的区别 <!DOCTYPE html> <html> <head> <meta char ...
- module_init和init_module的区别
今天在看CS8900的驱动时,发现其驱动的模块加载函数是init_module(),由于看到大多数的驱动用的模块加载函数大多是module_init()函数,所以一时没缓过神来,总是在找CS8900的 ...