几种常见Ruby on Rails内置方法介绍
Ruby on Rails是一个功能强大的WEB开发框架,在这里我们将会学到一些经常用到的Ruby on Rails内置方法,帮助大家熟练掌握其应用技巧。
Ruby on Rails内置方法之对象是否为空blank?
- puts [ ].blank? #=> true
- puts { 1 => 2}.blank? #=> false
- puts " cat ".blank? #=> false
- puts "".blank? #=> true
- puts " ".blank? #自动去除前后空格=> true
- puts nil.blank? #=> true
Ruby on Rails内置方法之排序:group_by()
groups = posts.group_by {|post| post.author_id}
Ruby on Rails内置方法之组句:to_sentence()
puts [ "ant", "bat", "cat"]. to_sentence #=> "ant, bat, and cat" puts [ "ant", "bat", "cat"]. to_sentence(:connector => "and not forgetting") #=> "ant, bat, and not forgetting cat" puts [ "ant", "bat", "cat"]. to_sentence(:skip_last_comma => true) #=> "ant, bat and cat"
Ruby on Rails内置方法之分组:in_groups_of()
[1,2,3,4,5,6,7].in_groups_of(3) {|slice| p slice} #=> [1, 2, 3] [4, 5, 6] [7,nil, nil] [1,2,3,4,5,6,7].in_groups _of(3,"X") {|slice| p slice} #=> [1, 2, 3] [4, 5, 6] [7,"X", "X"]
相关推荐
chenshuixian 2013-06-01
何志文 2020-05-11
JOO 2020-04-26
龙浩然 2014-06-03
yohunl 2008-01-11
wujiajax 2011-07-22
samllQ的备忘录 2011-07-22
wl00 2020-10-28
EricNet 2020-07-05
EricNet 2020-05-27
happyfreeangel 2020-04-09
Poisedflw 2020-03-23
yangliuhbhd 2020-03-06
Ben的程序员生涯 2013-06-01
wes0 2014-05-31
mrice00 2019-12-20
EricNet 2019-12-11