ActionSupport in Rails4 Part1

Recently i study documents in ruby edgeguides. There are a lot of methods in ActionSupport chapter. I record something useful here and for reference in the future.

Extensions to All Objects


Defined in active_support/core_ext/object/blank.rb.

blank?

Following vlaues are considered to be blank.
* nil and false
* strings composed only of whitespace (see note below)
* empty arrays and hashes
In particular, 0 and 0.0 are not blank.

present?

The method present? is equivalent to !blank?

presence

Returns itself if present?, and nil otherwise. Useful

1
host = config[:host].presence || 'localhost'

Defined in active_support/core_ext/object/duplicable.rb.

duplicable?

By definition all objects are duplicable? except nil, false, true, symbols, numbers, class, and module objects.


Defined in active_support/core_ext/object/duplicable.rb.

deep_dup

Deep duplicate. Normally, dup an object that contains other objects, Ruby only creates a shallow copy of the object.


Defined in active_support/core_ext/object/try.rb.

try

Try to call a method only if it is not nil

1
2
3
4
5
6
7
# without try
unless @number.nil?
  @number.next
end

# with try
@number.try(:next)

try can also be called without arguments but a block

1
@person.try { |person| "#{person.first_name} #{person.last_name}" }

Defined in active_support/core_ext/object/with_options.rb.

with_options

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Account < ActiveRecord::Base
  has_many :customers, dependent: :destroy
  has_many :products,  dependent: :destroy
  has_many :invoices,  dependent: :destroy
  has_many :expenses,  dependent: :destroy
end

# with options way
class Account < ActiveRecord::Base
  with_options dependent: :destroy do |assoc|
    assoc.has_many :customers
    assoc.has_many :products
    assoc.has_many :invoices
    assoc.has_many :expenses
  end
end

Defined in active_support/core_ext/object/instance_variables.rb.

instance_values

1
2
3
4
5
6
7
class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}

instance_variable_names

1
2
3
4
5
6
7
class C
  def initialize(x, y)
    @x, @y = x, y
  end
end

C.new(0, 1).instance_variable_names # => ["@x", "@y"]

Defined in active_support/core_ext/object/inclusion.rb.

in?

1
2
3
4
1.in?([1,2])        # => true
"lo".in?("hello")   # => true
25.in?(30..50)      # => false
1.in?(1)            # => ArgumentError because argument passed does not respond to include?.

Extensions to Module


Defined in active_support/core_ext/module/attr_internal.rb.

Internal Attributes

Active Support defines the macros attr_internal_reader, attr_internal_writer, and attr_internal_accessor The macro attr_internal is a synonym for attr_internal_accessor:

1
2
3
4
5
6
7
8
9
# library
class ThirdPartyLibrary::Crawler
  attr_internal :log_level
end

# client code
class MyCrawler < ThirdPartyLibrary::Crawler
  attr_accessor :log_level
end

Extensions to String


Defined in active_support/core_ext/string/filters.rb.

remove

1
Hello World".remove(/Hello /) => "World"

squish

The method squish strips leading and trailing whitespace, and substitutes runs of whitespace with a single space each:

1
" \n  foo\n\r \t bar \n".squish # => "foo bar"

truncate

1
2
"Oh dear! Oh dear! I shall be late!".truncate(20)
# => "Oh dear! Oh dear!..."

Ellipsis can be customized with the :omission option

1
2
"Oh dear! Oh dear! I shall be late!".truncate(20, omission: '&hellip;')
# => "Oh dear! Oh &hellip;"

Note in particular that truncation takes into account the length of the omission string. Pass a :separator to truncate the string at a natural break:

1
2
3
4
"Oh dear! Oh dear! I shall be late!".truncate(18)
# => "Oh dear! Oh dea..."
"Oh dear! Oh dear! I shall be late!".truncate(18, separator: ' ')
# => "Oh dear! Oh..."

Defined in active_support/core_ext/string/starts_ends_with.rb.

starts_with? and ends_with?

1
2
"foo".starts_with?("f") # => true
"foo".ends_with?("o")   # => true

Defined in active_support/core_ext/string/access.rb.

at(position)

1
2
3
4
"hello".at(0)  # => "h"
"hello".at(4)  # => "o"
"hello".at(-1) # => "o"
"hello".at(10) # => nil

from(position)

1
2
3
4
"hello".from(0)  # => "hello"
"hello".from(2)  # => "llo"
"hello".from(-2) # => "lo"
"hello".from(10) # => "" if < 1.9, nil in 1.9

to(position)

1
2
3
4
"hello".to(0)  # => "h"
"hello".to(2)  # => "hel"
"hello".to(-2) # => "hell"
"hello".to(10) # => "hello"

Defined in active_support/core_ext/string/conversions.rb.

to_date, to_time, to_datetime

1
2
3
4
5
6
"2010-07-27".to_date              # => Tue, 27 Jul 2010
"2010-07-27 23:37:00".to_time     # => Tue Jul 27 23:37:00 UTC 2010
"2010-07-27 23:37:00".to_datetime # => Tue, 27 Jul 2010 23:37:00 +0000

"2010-07-27 23:42:00".to_time(:utc)   # => Tue Jul 27 23:42:00 UTC 2010
"2010-07-27 23:42:00".to_time(:local) # => Tue Jul 27 23:42:00 +0200 2010

Comments