C08 Exceptions
1. 定义一个Exception
class MyException < StandardError
def initialize(info)
super(info)
end
end
2. 抛出并且捕获一个异常
require 'my_exception'
def raise_exception
raise MyException.new("ERROR occurs!!!")
end
begin
raise_exception()
rescue MyException => e
print(e.backtrace.join("\n"))
end
3. ensure和else
require 'my_exception'
def raise_exception_bycondition(num)
if num != 5
puts("num = #{num}")
elsif num == 5
puts("num = 5")
raise MyException.new("Num == 5!!!")
end
end
print("Input a number:")
end_num = Integer(gets)
begin
for i in 1..end_num
raise_exception_bycondition(i)
end
rescue MyException => e
print(e.backtrace.join("\n"))
puts()
else
puts("No MyException!")
ensure
puts("Alwasy ouput this sentense!")
end
4. Retry
require 'my_exception'
def raise_exception_bycondition(raiseornot)
if raiseornot
raise MyException.new("Raise MyException!")
else
puts("Don't raise MyExcetpion!")
end
end
$raiseornot = true
begin
raise_exception_bycondition($raiseornot)
rescue MyException => e
print(e.backtrace.join("\n"))
puts()
$raiseornot = false
retry
end
5. throw and catch
def prompt_and_get(prompt)
print prompt
res = readline.chomp
throw :quit_requested if res == "!"
res
end
catch :quit_requested do
name = prompt_and_get("Name: ")
age = prompt_and_get("Age: ")
sex = prompt_and_get("Sex: ")
puts("name: #{name} -- Age: #{age} -- Sex: #{sex}")
end
6. caller在raise时的使用
require 'my_exception'
def raise_exception
raise MyException, "ERROR occurs!!!", caller[1..-1]
end
def raise_exception2
raise_exception
end
begin
raise_exception2()
rescue MyException => e
print(e.backtrace.join("\n"))
puts()
end
结果显示是:
caller_sample.rb:12
C09 Modules
1. Module
#module1.rb
module Module1
Name = "Module1 Name"
def Module1.info
"Module1"
end
end
#module2.rb
module Module2
Name = "Module2 Name"
def Module2.info
"Module2"
end
end
#module_sample.rb
require 'module1'
require 'module2'
puts(Module1.info)
puts(Module2.info)
puts(Module1::Name)
puts(Module2::Name)
2. Mixin
#observable_mixin.rb
module Observable
def observers
@observer_list ||= []
end
def add_observer(obj)
observers << obj
end
def notify_observers
observers.each {|o| o.update }
end
end
#weather_info.rb
require 'observable_mixin'
class WeatherInfo
include Observable
end
class WeatherShowForm1
def update
puts 'Weather update1'
end
end
class WeatherShowForm2
def update
puts 'Weather update2'
end
end
winfo = WeatherInfo.new()
winfo.add_observer(WeatherShowForm1.new)
winfo.add_observer(WeatherShowForm2.new)
winfo.notify_observers()
3. 名字查找顺序
Ruby looks first in the immediate class of an object, then in the mixins included into that class, and then in superclasses and their mixins. If a class has multiple modules mixed in, the last one included is searched first.
分享到:
相关推荐
Programming Ruby 2nd Edition
学习ruby的绝佳入门书籍,Programming Ruby 2nd.pdf
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
NULL 博文链接:https://huihuilou.iteye.com/blog/1997808
Programming Ruby 2nd Edition
上传一系列RUBY的书,基本上网上能见到的全了
Programming Ruby中文版第二版[高清扫描版][带书签]。 Programming.Ruby-2nd[高清文字版][带
Pragmatic Programmers - Programming Ruby(2nd Edition Syngress).rar
内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm
Programming Ruby 2.0 1.9 pdf
Programming Ruby, Ruby程序设计
Programming Ruby 1.9 (3rd edition)和源码
Programming ruby.pdf 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
对于初学C的人,这本书是很好的教材,但也有需要记录的学习经验
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
Programming Ruby English version and the source Code
PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版
Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf