I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:
-
ActiveSupport::Callbacks
Show archive.org snapshot -
ActiveRecord-like callbacks, if you need callbacks in non ActiveRecord objects
-
ActiveSupport::MessageEncryptor
Show archive.org snapshot -
encrypt and decrypt ruby objects
-
ActiveSupport::MessageVerifier
Show archive.org snapshot -
sign and verify ruby objects
-
ActiveSupport::OrderedOptions
Show archive.org snapshot -
like an ordered hash, but the keys can also be accessed like methods
h = ActiveSupport::OrderedOptions.new
h['foo'] = 'bar'
h.foo = 'bar' # same thing
h['foo'] # => 'bar'
h.foo # => 'bar'
-
in?
Show archive.org snapshot (> 3.0 only) -
reverse of
Array#include?
characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true
-
Array.wrap
Show archive.org snapshot -
wraps argument in an array, unless it is array-like already. The behaviour is a bit saner than
Array[...]
.
Array.wrap(nil) # => []
Array.wrap([1,2,3]) # => [1,2,3]
Array.wrap(1) # => [1]
Array.wrap("foo \n bar") # => ["foo \n bar"]
-
Class.class_attributes
Show archive.org snapshot (> 3.0 only) -
inheritable class attributes. Subclasses inherit from their parents, but cannot override their values
class Base
class_attribute :setting
end
class Subclass < Base
end
Base.setting = true
Subclass.setting # => true
Subclass.setting = false
Subclass.setting # => false
Base.setting # => true
-
Float#round
Show archive.org snapshot -
round to a given number of decimals
1.2345.round(2) # => 1.23
1.2345.round(3) # => 1.235
-
Hash#deep_merge(!)
Show archive.org snapshot -
merge hashes recursively
-
String#squish(!)
Show archive.org snapshot -
removes leading and trailing whitespace, and converts all internal whitespace to single spaces
%{ Multi-line
string }.squish # => "Multi-line string"
" foo bar \n \t boo".squish # => "foo bar boo"
-
Time.current
,Date.current
,DateTime.current
Show archive.org snapshot -
behaves like
.now
or.zone.now
depending on whether a time zone is set