class Object
def `(command) #: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)
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:
def as_json(options = nil) #:nodoc: if respond_to?(:to_hash) to_hash.as_json(options) else instance_values.as_json(options) end end
def blank?
-
(true, false)
-
def blank? respond_to?(:empty?) ? !!empty? : !self end
def deep_dup
object.instance_variable_defined?(:@a) # => false
dup.instance_variable_set(:@a, 1)
dup = object.deep_dup
object = Object.new
not duplicable, returns +self+.
Returns a deep copy of object if it's duplicable. If it's
def deep_dup duplicable? ? dup : self end
def duplicable?
False for method objects;
Can you safely dup this object?
def duplicable? true end
def html_safe?
def html_safe? false end
def in?(another_object)
This will throw an +ArgumentError+ if the argument doesn't respond
"Konata".in?(characters) # => true
characters = ["Konata", "Kagami", "Tsukasa"]
any object which responds to +#include?+. Usage:
Returns true if this object is included in the argument. Argument must be
def in?(another_object) another_object.include?(self) rescue NoMethodError raise ArgumentError.new("The parameter passed to #in? must respond to #include?") end
def instance_values
end
end
@x, @y = x, y
def initialize(x, y)
class C
corresponding values.
Returns a hash with string keys that maps instance variable names without "@" to their
def instance_values Hash[instance_variables.map { |name| [name[1..-1], instance_variable_get(name)] }] end
def instance_variable_names
end
end
@x, @y = x, y
def initialize(x, y)
class C
Returns an array of instance variable names as strings including "@".
def instance_variable_names instance_variables.map(&:to_s) end
def presence
-
(Object)
-
def presence self if present? end
def presence_in(another_object)
-
(Object)
-
def presence_in(another_object) self.in?(another_object) ? self : nil end
def present?
-
(true, false)
-
def present? !blank? end
def to_param
def to_param to_s end
def to_query(key)
Converts an object into a string suitable for use as a URL query string,
def to_query(key) "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}" end
def with_options(options, &block)
Hence the inherited default for `if` key is ignored.
validates :content, length: { minimum: 50 }, if: -> { content.present? }
The code is equivalent to:
end
end
validates :content, if: -> { content.present? }
with_options if: :persisted?, length: { minimum: 50 } do
class Post < ActiveRecord::Base
NOTE: 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
end
has_many :expenses
has_many :invoices
has_many :products
has_many :customers
with_options dependent: :destroy do
class Account < ActiveRecord::Base
in merging options context:
When you don't pass an explicit receiver, it executes the whole block
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, &block) option_merger = ActiveSupport::OptionMerger.new(self, options) block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger) end