Read more

Random list of ActiveSupport goodies

Tobias Kraze
August 15, 2011Software engineer at makandra GmbH

I recently browsed through the ActiveSupport code and found some nice stuff I did not know about:

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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

Tobias Kraze
August 15, 2011Software engineer at makandra GmbH
Posted by Tobias Kraze to makandra dev (2011-08-15 17:25)