摘要:
可以用{}书写一个block,如 {puts "hello"}也可以不用{},直接写为puts "hello"或用begin puts "hello"end在每一行可以加;puts "hello";puts "hey";或不加;puts "hello"puts "hey"或在do..end中定义do club.enroll(person) person.socializeend另外if radiation>3000 puts "Danger 阅读全文
摘要:
ruby中有类似vb的模块module如:module Summable def sum inject {|v,n| v+n} endendclass Array include Summableendclass Range include Summableendclass VowelFinder include Summableend[1,2,3,4,5].sum 得到 15('a'..'m') 得到 "abcdefghijklm"vf=VowelFinder.new("the quick brown fox jumped&quo 阅读全文
摘要:
class File def File.Open(*args) result=f=File.new(*args) if block_given? begin result=yield f ensure f.close end return result endend注: ensure用于必定要执行的语句 阅读全文