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

排序:group_by()
groups = posts.group_by {|post| post.author_id}

组句: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"

分组: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"]

字符串方法:
string = "Now is the time"
puts string.at(2) #=> "w"
puts string.from(8) #=> "he time"
puts string.to(8) #=> "Now is th"
puts string.first #=> "N"
puts string.first(3) #=> "Now"
puts string.last #=> "e"
puts string.last(4) #=> "time"
puts string.starts_with?("No") #=> true
puts string.ends_with?("ME") #=> false

count = Hash.new(0)
string.each_char {|ch| count[ch] += 1}
p count #=> {" "=>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 #=> "First name"
puts "now is the time".titleize #=> "Now Is The Time"
你可以自定义特定单词的单复数转换规则

数字方法:
puts 3.ordinalize #=> "3rd"
puts 321.ordinalize #=> "321st"

数量方法:
puts 20.bytes #=> 20
puts 20.kilobytes #=> 20480
puts 20.megabytes #=> 20971520
puts 20.gigabytes #=> 21474836480
puts 20.terabytes #=> 21990232555520
puts 20.petabytes #=> 22517998136852480
puts 1.exabyte #=> 1152921504606846976

时间方法:
puts 20.seconds #=> 20
puts 20.minutes #=> 1200
puts 20.hours #=> 72000
puts 20.days #=> 1728000
puts 20.weeks #=> 12096000
puts 20.fortnights #=> 24192000
puts 20.months #=> 51840000
puts 20.years #=> 630720000

puts Time.now #=> Thu May 18 23:29:14 CDT 2006
puts 20.minutes.ago #=> Thu May 18 23:09:14 CDT 2006
puts 20.hours.from_now #=> Fri May 19 19:29:14 CDT 2006
puts 20.weeks.from_now #=> Thu Oct 05 23:29:14 CDT 2006
puts 20.months.ago #=> Sat Sep 25 23:29:16 CDT 2004
puts 20.minutes.until("2006-12-25 12:00:00".to_time)
#=> Mon Dec 25 11:40:00 UTC 2006
puts 20.minutes.since("2006-12-25 12:00:00".to_time)
#=> Mon Dec 25 12:20:00 UTC 2006

now = Time.now
puts now #=> Thu May 18 23:36:10 CDT 2006
puts now.to_date #=> 2006-05-18
puts now.to_s #=> Thu May 18 23:36:10 CDT 2006
puts now.to_s(:short) #=> 18 May 23:36
puts now.to_s(:long) #=> May 18, 2006 23:36
puts now.to_s(:db) #=> 2006-05-18 23:36:10
puts now.to_s(:rfc822) #=> Thu, 18 May 2006 23:36:10 -0500
puts now.ago(3600) #=> Thu May 18 22:36:10 CDT 2006
puts now.at_beginning_of_day #=> Thu May 18 00:00:00 CDT 2006
puts now.at_beginning_of_month #=> Mon May 01 00:00:00 CDT 2006
puts now.at_beginning_of_week #=> Mon May 15 00:00:00 CDT 2006
puts now.beginning_of_quarter #=> Sat Apr 01 00:00:00 CST 2006
puts now.at_beginning_of_year #=> Sun Jan 01 00:00:00 CST 2006
puts now.at_midnight #=> Thu May 18 00:00:00 CDT 2006
puts now.change(:hour => 13) #=> Thu May 18 13:00:00 CDT 2006
puts now.last_month #=> Tue Apr 18 23:36:10 CDT 2006
puts now.last_year #=> Wed May 18 23:36:10 CDT 2005
puts now.midnight #=> Thu May 18 00:00:00 CDT 2006
puts now.monday #=> Mon May 15 00:00:00 CDT 2006
puts now.months_ago(2) #=> Sat Mar 18 23:36:10 CST 2006
puts now.months_since(2) #=> Tue Jul 18 23:36:10 CDT 2006
puts now.next_week #=> Mon May 22 00:00:00 CDT 2006
puts now.next_year #=> Fri May 18 23:36:10 CDT 2007
puts now.seconds_since_midnight #=> 84970.423472
puts now.since(7200) #=> Fri May 19 01:36:10 CDT 2006
puts now.tomorrow #=> Fri May 19 23:36:10 CDT 2006
puts now.years_ago(2) #=> Tue May 18 23:36:10 CDT 2004
puts now.years_since(2) #=> Sun May 18 23:36:10 CDT 2008
puts now.yesterday #=> Wed May 17 23:36:10 CDT 2006
puts now.advance(:days => 30) #=> Sat Jun 17 23:36:10 CDT 2006
puts Time.days_in_month(2) #=> 28
puts Time.days_in_month(2, 2000) #=> 29

日期方法:
date = Date.today
puts date.to_s #=> "2006-05-18"
puts date.to_time #=> Thu May 18 00:00:00 CDT 2006
puts date.to_s(:short) #=> "18 May"
puts date.to_s(:long) #=> "May 18, 2006"
puts date.to_s(:db) #=> "2006-05-18"

将字符串转成时间日期:
puts "2006-12-25 12:34:56".to_time #=> Mon Dec 25 12:34:56 UTC 2006
puts "2006-12-25 12:34:56".to_date #=> 2006-12-25

转载请注明: 转自船长日志, 本文链接地址: http://www.cslog.cn/Content/ruby_on_rails_active_methods/

此条目发表在 Ruby on Rails 分类目录。将固定链接加入收藏夹。

发表评论