[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 ...
随机推荐
- php数组(array)输出三种形式
$bbbb=array("11"=>"aaa","22"=>"bbb"); //只能输出值value不能输出 ...
- Oracle数据库之PL/SQL触发器
Oracle数据库之PL/SQL触发器 1. 介绍 触发器(trigger)是数据库提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是 ...
- Oracle 面试宝典 - General Questions
转自 http://www.orafaq.com/wiki/Interview_Questions Tell us about yourself/ your background. What are ...
- Chatwin商务通
<script language="javascript" src="http://lwt.zoosnet.net/JS/LsJS.aspx?siteid=你商务通 ...
- illegal mix of collcations表连接时非法的校对
背景:旧表导入新表,新表里的字段是字符串类型 新表是int类型 两个字段通过字符串处理后相等 (准备left join 关联起来)报错 把int类型字段更改成varchar字符串类型后成功
- eclipse4.2 UI换回 3.6版本的UI
Apparently, the Eclipse developers were kind enough to leave us an easy way out: From the Window men ...
- 删除svn文件
删除svn文件 sudo find . -name ".DS_Store" -exec rm -r {} \; sudo find . -name ".git" ...
- WebKit JavaScript Binding添加新DOM对象的三种方式
一.基础知识 首先WebKit IDL并非完全遵循Web IDL,只是借鉴使用.WebKit官网提供了一份说明(WebKitIDL),比如Web IDL称"operation”(操作), 而 ...
- Apache HTTPServer与JBoss/Tomcat的整合与请求分发
http://www.blogjava.net/supercrsky/archive/2008/12/24/248143.html
- 24篇Delphi文件操作文章
http://www.cnblogs.com/keyvip/category/268043.html