对象内容判断.
nil 和 false 对象 ,返回 true.
数值 和 TRUE 则始终返回 false.
其它对象,如为空时,返回 true,
如对象是字符串,会先剥离前后空格,再进行检查是否为空.
puts [ ].blank? #=> true
puts { 1 =>2}.blank? #=>false
puts "cat".blank? #=>false
puts "".blank? #=>true
puts " ".blank? #=>true
puts nil.blank? #=>true
枚举与数组.Enumerations and Arrays
groups = posts.group_by {post|post.author_id}
us_states = state.find(:all)
state_lookup = us_states.index_by(|state| state.short_name)
puts[ "ant", "bat", "cat"].to_sentence #=>"ant,bat,and cat"
puts[ "ant", "bat", "cat"].to_sentence(:connector=> "andnotforgetting")
#=>"ant,bat,and not forgetting cat"
puts[ "ant", "bat", "cat"].to_sentence(:skip_last_comma=> true)
#=>"ant,bat and cat"
[1,2,3,4,5,6,7].in_groups_of(3) {|slice|putsslice.inspect}
#=>[1,2,3]
[4,5,6]
[7,nil, nil]
[1,2,3,4,5,6,7].in_groups_of(3,"X"){|slice|putsslice.inspect}
#=>[1,2,3]
[4,5,6]
[7,"X", "X"]
字符串扩展 String Extensions
string= "Nowisthetime"
putsstring.at(2) #=>"w"
putsstring.from(8) #=>"hetime"
putsstring.to(8) #=>"Nowisth"
putsstring.first #=>"N"
putsstring.first(3) #=>"Now"
putsstring.last #=>"e"
putsstring.last(4) #=>"time"
putsstring.starts_with?("No") #=>true
putsstring.ends_with?("ME") #=>false
count=Hash.new(0)
string.each_char{|ch|count[ch]+=1}
putscount.inspect
#=>{""=>3,"w"=>1,"m"=>1,"N"=>1,"o"=>1,
"e"=>2, "h"=>1, "s"=>1, "t"=>2, "i"=>2}
puts "cat".pluralize #=>cats
puts "cats".pluralize #=>cats
puts "erratum".pluralize #=>errata
puts "cats".singularize #=>cat
puts "errata".singularize #=>erratum
puts "first_name".humanize #=>"Firstname"
puts "nowisthetime".titleize #=>"NowIsTheTime"
depot> rubyscript/console
Loadingdevelopmentenvironment(Rails2.1).
>>"goose".pluralize
=>"gooses"