C05 Standard Types
1. String
print <<STRING1,<<STRING2
Concat
STRING1
enate
STRING2
produces:
Concat
enate
2. Range
a = 1..3 #a = 1,2,3
a = 1...3 # a = 1,2
(1..10) === 5 ! true
(1..10) === 15 ! false
(1..10) === 3.14159 ! true
('a'..'j') === 'c' ! true
('a'..'j') === 'z' ! false
class UV
include Comparable
attr :volume
def initialize(volume) # 0..9
@volume = volume
end
def inspect
'#' * @volume
end
# Support for ranges
def <=>(other)
self.volume <=> other.volume
end
def succ
raise(IndexError, "Volume too big") if @volume >= 9
VU.new(@volume.succ)
end
end
(1..10) === 15 -> false
(1..10) === 3.14159 -> true
('a'..'j') === 'c' -> true
('a'..'j') === 'z' -> false
3. Regular Expression
re = /(\d+):(\d+)/ # match a time hh:mm
md = re.match("Time: 12:34am")
md.class -> MatchData
md[0] # == $& -> "12:34"
md[1] # == $1 -> "12"
md[2] # == $2 -> "34"
md.pre_match # == $` -> "Time: "
md.post_match # == $' -> "am"
C06 Method
1. 可变参数
def varargs(arg1, *rest)
"Got #{arg1} and #{rest.join(', ')}"
end
puts(varargs("one", "two", "three"))
2. Block
def take_block(p1)
if block_given?
yield(p1)
else
p1
end
end
puts(take_block("no block"))
puts(take_block("no block"){|x| x.sub(/no /, '')})
class TaxCalculator
def initialize(name, &block)
@name = name
@block = block
end
def get_tax(amount)
"#{@name} on #{@amount} = #{@block.call(amount)}"
end
end
tc = TaxCalculator.new("Sales Tax"){|amt| amt * 0.075}
puts(tc.get_tax(100))
3. Hash参数
def match_score(time, scores)
puts("Match score on #{time}")
scores.each do |name, score|
puts("#{name} -- #{score}")
end
end
match_score('2009-03-15',
'Zhibin' => 7,
'Henry' => 3,
'Keyu' => 2
)
4. Lambda
print('(t)imes or (p)lus')
times = gets
print('number:')
number = Integer(gets)
if times =~ /^t/
calc = lambda { |n| n * number }
else
calc = lambda { |n| n + number }
end
puts((1..10).collect(&calc).join(', '))
C07 Expressions
1. Overload operator
class Fixnum
alias old_plus +
def +(other)
old_plus(other).succ
end
end
puts(1+2)
2. 执行命令
puts(`date`)
puts($?)
puts(%x{echo 'hello world'})
puts($?)
puts(`./test.x`)
puts($?)
3. 并行赋值
b, (c, d), e = 1,2,3,4 -> b == 1, c == 2, d == nil, e == 3
b, (c, d), e = [1,2,3,4] -> b == 1, c == 2, d == nil, e == 3
b, (c, d), e = 1,[2,3],4 -> b == 1, c == 2, d == 3, e == 4
b, (c, d), e = 1,[2,3,4],5 -> b == 1, c == 2, d == 3, e == 5
b, (c,*d), e = 1,[2,3,4],5 -> b == 1, c == 2, d == [3, 4], e == 5
4. Case
def vowel_count(word)
count = 0
word.each_char do |c|
case c
when 'a'
count += 1
when 'e'
count += 1
when 'i'
count += 1
when 'o'
count += 1
when 'u'
count += 1
end
end
count
end
puts(vowel_count('I love u!'))
5. Variable Scope
while, for, if, util 这些都不会带来新的作用域,可是block的使用,会带来新的作用域。
分享到:
- 2009-03-15 18:37
- 浏览 910
- 评论(0)
- 论坛回复 / 浏览 (0 / 1594)
- 查看更多
相关推荐
Programming Ruby 2nd Edition
学习ruby的绝佳入门书籍,Programming Ruby 2nd.pdf
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
Programming Ruby 2nd Edition
NULL 博文链接:https://huihuilou.iteye.com/blog/1997808
上传一系列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》中文第2版源代碼下載
Programming Ruby, Ruby程序设计
Programming Ruby 1.9 (3rd edition)和源码
Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf Boost.Asio C++ Network Programming(2nd).pdf
Programming ruby.pdf 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
对于初学C的人,这本书是很好的教材,但也有需要记录的学习经验
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
Programming Ruby English version and the source Code