Updated at: February 23, 2017
语法糖(Syntactic sugar)指计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使用。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会
(1..10).map{|n| n*2}
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[1,2,3].rotate
[1,2,3].rotate
=[2,3,1]
(1..1000).inject{|sum,n| sum+n}
=> 500500
(1..1000).inject(:+)
=> 500500
[1,2,3,4].partition{|n| n>2}
=> [[3, 4], [1, 2]]
[1,1,2,3,3,4,5].uniq!.reject!{ |i| i%2 == 0 }.reverse
=> [5, 3, 1]
search=['a','b']
target="a and b"
search.any? {|s| target.include?(s)}
=> true
file_text=File.read(".kshrc")
file_lines=File.readlines(".kshrc").map{|l| l.chop}
4.times{|n| puts "Happy Birthday #{n==2? "dear xxx" : "to You"}"}
Happy Birthday to You
Happy Birthday to You
Happy Birthday dear xxx
Happy Birthday to You