wash

          Active Support

          Active Support is a set of libraries that are shared by all Rails components.
          Much of what’s in there is intended for Rails internal use. However, Active
          Support also extends some of Ruby’s built-in classes in interesting and
          useful ways. In this section we’ll quickly list the most popular of these
          extensions.
          Extensions to Numbers
          Class Fixnum gains the two instance methods even? and odd?.
          All numeric objects gain a set of scaling methods.
          puts 20.bytes #=> 20
          puts 20.kilobytes #=> 20480
          puts 20.megabytes #=> 20971520
          puts 20.gigabytes #=> 21474836480
          puts 20.terabytes #=> 21990232555520
          There are also time-based scaling methods. These convert their receiver
          into the equivalent number of seconds. The months( ) and years( ) methods
          are approximations—months are assumed to be 30 days long, years 365
          days long.
          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
          You can also calculate times relative to Time.now using the methods ago( )
          and from_now( ) (or their aliases until( ) and since( ), respectively).
          puts Time.now #=> Tue May 10 17:03:43 CDT 2005
          puts 20.minutes.ago #=> Tue May 10 16:43:43 CDT 2005
          puts 20.hours.from_now #=> Wed May 11 13:03:43 CDT 2005
          puts 20.weeks.from_now #=> Tue Sep 27 17:03:43 CDT 2005
          puts 20.months.ago #=> Thu Sep 18 17:03:43 CDT 2003
          How cool is that?
          Time Extensions
          The Time class gains a number of useful methods, helping you calculate
          relative times.
          now = Time.now
          puts now #=> Tue May 10 17:15:59 CDT 2005
          puts now.ago(3600) #=> Tue May 10 16:15:59 CDT 2005
          puts now.at_beginning_of_day #=> Tue May 10 00:00:00 CDT 2005
          puts now.at_beginning_of_month #=> Sun May 01 00:00:00 CDT 2005
          puts now.at_beginning_of_week #=> Mon May 09 00:00:00 CDT 2005
          puts now.at_beginning_of_year #=> Sat Jan 01 00:00:00 CST 2005
          puts now.at_midnight #=> Tue May 10 00:00:00 CDT 2005
          puts now.change(:hour => 13) #=> Tue May 10 13:00:00 CDT 2005
          puts now.last_month #=> Sun Apr 10 17:15:59 CDT 2005
          puts now.last_year #=> Mon May 10 17:15:59 CDT 2004
          puts now.midnight #=> Tue May 10 00:00:00 CDT 2005
          puts now.monday #=> Mon May 09 00:00:00 CDT 2005
          puts now.months_ago(2) #=> Thu Mar 10 17:15:59 CST 2005
          puts now.months_since(2) #=> Sun Jul 10 17:15:59 CDT 2005
          puts now.next_week #=> Mon May 16 00:00:00 CDT 2005
          puts now.next_year #=> Wed May 10 17:15:59 CDT 2006
          puts now.seconds_since_midnight #=> 62159.215938
          puts now.since(7200) #=> Tue May 10 19:15:59 CDT 2005
          puts now.tomorrow #=> Wed May 11 17:15:59 CDT 2005
          puts now.years_ago(2) #=> Sat May 10 17:15:59 CDT 2003
          puts now.years_since(2) #=> Thu May 10 17:15:59 CDT 2007
          puts now.yesterday #=> Mon May 09 17:15:59 CDT 2005
          Active Support also includes a TimeZone class. TimeZone objects encapsulate
          the names and offset of a time zone. The class contains a list of the
          world’s time zones. See the Active Support RDoc for details.
          String Extensions
          Active Support adds methods to all strings to support the way the Rails
          core converts names from singular to plural, lowercase to mixed case, and
          so on. Of these, two might be useful in the average application.
          puts "cat".pluralize #=> cats
          puts "cats".pluralize #=> cats
          puts "erratum".pluralize #=> errata
          puts "cats".singularize #=> cat
          puts "errata".singularize #=> erratum

          posted on 2006-05-10 10:35 wash 閱讀(206) 評論(0)  編輯  收藏 所屬分類: ruby rails

          主站蜘蛛池模板: 毕节市| 伊通| 永兴县| 武威市| 且末县| 道孚县| 诸城市| 昌都县| 武邑县| 明水县| 无棣县| 科尔| 满洲里市| 灵川县| 林周县| 仁寿县| 高要市| 尉犁县| 内江市| 昌都县| 鹤壁市| 高邮市| 兰西县| 洱源县| 盐津县| 南康市| 朝阳市| 墨玉县| 泗水县| 太保市| 信宜市| 如东县| 盈江县| 依兰县| 丽水市| 江陵县| 武宣县| 东乌| 孟村| 正宁县| 马公市|