class Object

def `(command) #:nodoc:

:nodoc:
Unix on the former but not the latter.
spawned shell prints a message to stderr and sets $?. We emulate
On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
Makes backticks behave (somewhat more) similarly on all platforms.
def `(command) #:nodoc:
  super
rescue Errno::ENOENT => e
  STDERR.puts "#$0: #{e}"
end

def acts_like?(duck)

we want to act like Time simply need to define an acts_like_time? method.
"x.acts_like?(:date)" to do duck-type-safe comparisons, since classes that
acts_like_time?. As a result, we can do "x.acts_like?(:time)" and
to define an acts_like_date? method, and extends Time to define
A duck-type assistant method. For example, Active Support extends Date
def acts_like?(duck)
  respond_to? :"acts_like_#{duck}?"
end

def as_json(options = nil) #:nodoc:

:nodoc:
def as_json(options = nil) #:nodoc:
  if respond_to?(:to_hash)
    to_hash
  else
    instance_values
  end
end

def blank?

if address.blank?

...to:

if address.nil? || address.empty?

This simplifies:

For example, "", " ", +nil+, [], and {} are all blank.
An object is blank if it's false, empty, or a whitespace string.
def blank?
  respond_to?(:empty?) ? empty? : !self
end

def duplicable?

true otherwise.
False for +nil+, +false+, +true+, symbols, numbers, class and module objects;

Can you safely dup this object?
def duplicable?
  true
end

def html_safe?

def html_safe?
  false
end

def in?(*args)

to +#include?+.
This will throw an ArgumentError if a single argument is passed in and it doesn't respond

character.in?("Konata", "Kagami", "Tsukasa") # => true
character = "Konata"

"Konata".in?(characters) # => true
characters = ["Konata", "Kagami", "Tsukasa"]

any object which responds to +#include?+ or optionally, multiple arguments can be passed in. Usage:
Returns true if this object is included in the argument(s). Argument must be
def in?(*args)
  if args.length > 1
    args.include? self
  else
    another_object = args.first
    if another_object.respond_to? :include?
      another_object.include? self
    else
      raise ArgumentError.new("The single parameter passed to #in? must respond to #include?")
    end
  end
end

def instance_values #:nodoc:

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

end
end
@x, @y = x, y
def initialize(x, y)
class C

corresponding values. Keys are strings both in Ruby 1.8 and 1.9.
Returns a hash that maps instance variable names without "@" to their
def instance_values #:nodoc:
  Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }]
end

def instance_variable_names

def instance_variable_names
  instance_variables.map { |var| var.to_s }
end

def presence

region = params[:state].presence || params[:country].presence || 'US'

...becomes:

region = state || country || 'US'
country = params[:country] if params[:country].present?
state = params[:state] if params[:state].present?

HTTP POST/query parameters:
as not present at all. For example, this simplifies a common check for
This is handy for any representation of objects where blank is the same

object.presence is equivalent to object.present? ? object : nil.
Returns object if it's present? otherwise returns +nil+.
def presence
  self if present?
end

def present?

An object is present if it's not blank?.
def present?
  !blank?
end

def to_param

Alias of to_s.
def to_param
  to_s
end

def to_query(key)

Note: This method is defined as a default implementation for all Objects for Hash#to_query to work.

param name.
Converts an object into a string suitable for use as a URL query string, using the given key as the
def to_query(key)
  require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
  "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
end

def try(*a, &b)

+try+ behaves like +Object#send+, unless called on +NilClass+.
--
@person.try { |p| "#{p.first_name} #{p.last_name}" }
Without a method argument try will yield to the block unless the receiver is nil.

@people.try(:collect) {|p| p.name}
Person.try(:find, 1)
+try+ also accepts arguments and/or a block, for the method it is trying

@person.try(:name)
With +try+

@person ? @person.name : nil
or
@person && @person.name
Without +try+

==== Examples

delegate +try+ to target instead of calling it on delegator itself.
subclasses of +BasicObject+. For example, using try with +SimpleDelegator+ will
Please also note that +try+ is defined on +Object+, therefore it won't work with

If try is called without a method to call, it will yield any given block with the object.

and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
*Unlike* that method however, a +NoMethodError+ exception will *not* be raised

and/or the block specified, just like the regular Ruby Object#send does.
Invokes the method identified by the symbol +method+, passing it any arguments
def try(*a, &b)
  if a.empty? && block_given?
    yield self
  else
    __send__(*a, &b)
  end
end

def with_options(options)


Each nesting level will merge inherited defaults in addition to their own.
with_options can also be nested since the call is forwarded to its receiver.

end
body i18n.t :body, :user_name => user.name
subject i18n.t :subject
I18n.with_options :locale => user.locale, :scope => "newsletter" do |i18n|

It can also be used with an explicit receiver:

end
end
assoc.has_many :expenses
assoc.has_many :invoices
assoc.has_many :products
assoc.has_many :customers
with_options :dependent => :destroy do |assoc|
class Account < ActiveRecord::Base

Using with_options, we can remove the duplication:

end
has_many :expenses, :dependent => :destroy
has_many :invoices, :dependent => :destroy
has_many :products, :dependent => :destroy
has_many :customers, :dependent => :destroy
class Account < ActiveRecord::Base

Without with_options>, this code contains duplication:

hash as its final argument.
provided. Each method called on the block variable must take an options
the receiver, will have its options merged with the default +options+ hash
method calls. Each method called in the block, with the block variable as
An elegant way to factor duplication out of options passed to a series of
def with_options(options)
  yield ActiveSupport::OptionMerger.new(self, options)
end