Ruby bits ( 4 ):ActiveSupport

less than 1 minute read

install it and load it

install it

gem install activesupport gem install i18n

load it

require ‘active_support/all’

core extensions: array

array.form() array.to() array.in_group_of array.splite

core extensions: Date

.at_beginning_of_day .at_end_of_monthr

core extensions:hash

有兩個hash options = {user: ‘codeschool’, lang: ‘fr’} new_options = {user: ‘codeschool’, lang: ‘fr’, password: ‘dunno’}

options.diff(new_options)

比較option與new_options的不同

options.stringify_keys

turn keys into strings

Merge hash

會以前面的為主

remove keys

new_options.except(:password)

檢查是否有額外的key

new_options.assert_valid_keys(:user, :lang),如果有額外的key的話就會出現警示訊息

core extensions: integer

odd or even

index.odd? index.even?

ordinallize

”#{1.ordinalize} place!” “#{2.ordinalize} place!” “#{3.ordinalize} place!”

pluralize and singularize

轉換單字的單數與複數

titleize and humznize

延伸閱讀

看完還是有點霧煞煞,接著來看ihower實戰聖經:active support

Leave a Comment