the Zen of Ruby
||= - conditional asignment, 只在variable为nil时赋值
defind method 可省略return,it will return the result of the last evaluated expression.
.upto() / .downto() - 又一种遍历方法
.next - integer会返回下一个integer
#{ } - string interpolation, 在处理non-string时用
Control flow
case -> when -> when -> else -> end
Array
.push() - (srings也可以用) add an element to the end of an array #可以写成<<
Hash
set default value: Hash.new("default value")
.each_key / .each_value
.delete
Symbol
there's only one copy of any particular symbol at a given time.
用途: hash keys
referencing method names - e.g.
[1,2,3].respond_to?(:push)
symbols can't be changed once created
和string转化:.to_sym ( .intern ) , .to_s
*ruby1.9变化:创建hash时写成{one: 1},不用=>
.select - filter a hash for values that meet certain criteria
Block
blocks are not objects
.collect - returns a copy, dosen't mutate the origin value
yield - 此处运行block中的代码