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的更多相关文章

  1. HeadFIrst Ruby 第二章总结 methods and classes

    HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...

  2. Abstract Methods and Classes

    阅读了Java的官方Doc,在此总结如下. Abstract Class & Method In jave, "abstract" can be a modifier to ...

  3. ruby Methods, Procs, Lambdas, and Closures

    define simple method定义简单方法 关键字def用于方法定义,在其后是方法名和可选的参数名列表,参数名列表会用一对圆括号括住.构成方法主体的代码放在参数列表之后,end用于结束方法定 ...

  4. 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 ...

  5. 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 ...

  6. ruby中顶层定义的方法究竟放在哪里?

    ruby中顶层(top level)中定义的方法放在main中,证明如下: self.private_methods(false) #IN TOP LEVEL 那么methods方法究竟是在哪定义的, ...

  7. 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 ...

  8. TIJ——Chapter Seven:Reusing Classes

    Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...

  9. 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 ...

随机推荐

  1. 菜鸟必备教程,ajax与xml交互传输数据。

    今天,公司让学习ajax,然而我并不会,着急到爆炸,boom~~啥卡拉咔.看着教程一步一步摸索,写出来交互页面,写代码真的好惆怅啊. 额,不说废话,下面是源代码. 首先是ajax的代码,注释真的很重要 ...

  2. JqGrid自定义toolbar

    1.设置toolbar参数为[true,"top"],其意思是toolbar显示在Grid顶部,且其id为t_+Grid的id.e.g.: Grid的id为myGrid,toolb ...

  3. 全部与精简切换显示jQuery实例教程

    下面是某网站上的一个品牌列表展示效果,用户进入页面时,品牌列表默认是精简显示的(即不完整的品牌列表)效果如下图所示: 用户可以单击商品列表下方的“显示全部品牌”按钮来显示全部的品牌.单击“显示全部品牌 ...

  4. access_token的获取2

    概述 access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. access_token的存储至少要保留512个字符空间.acces ...

  5. GlusterFS简单配置

    1.准备工作 准备三台机器(物理机或者虚拟机均可)用于安装和测试GlusterFS,其中两台用作服务器,一台用作客户端,主机名分别为: Server1.zhaogang.int  10.0.21.24 ...

  6. Resilio-sync auto restart

    syncheck.sh #!/bin/sh if [ $(pgrep xxxrslsync) ]; then echo && date >> /home/pi/sync/s ...

  7. Egret HTTP网络

    HTTP 请求与响应: private createGameScene():void { //HTTP 1.0 var request = new egret.HttpRequest(); reque ...

  8. PLSQL Developer如何设置自动打开上次编辑的文件

    作为开发人员经常把sql语句保存到文件中以方便下次继续使用,问题是plsqlDev重启后每次都需要手工打开这个文件,好不方便: 以下设置是plsqlDev启动后自动打开上次编辑的文件. 选择配置> ...

  9. House of hello恶搞凯莉迷你包

    欧洲站 House of hello恶搞凯莉迷你包 最近淘宝卖的很疯,看看价格,俺咂舌不已 :1300-1600 今年迷你包卖的很疯我是知道的,迷你包今年没有买一个也是不行的! 剔除暴利,便宜的亲们不 ...

  10. CELL_PHOTO_IDENTIFIER

    # define CELL_PHOTO_IDENTIFIER @"photoLibraryCell" # define CLOSE_PHOTO_IMAGE @"close ...