跳到内容 跳到搜索
方法
A
B
D
H
I
P
T
W
包含的模块

常量

APP_PATH = File.expand_path("test/dummy/config/application", ENGINE_ROOT)
 

实例公共方法

acts_like?(duck)

提供一种方法来检查某个类是否基于适当命名的标记方法的存在而像其他类一样行事。

一个提供与 SomeClass 相同接口的类可以定义一个名为 acts_like_some_class? 的标记方法,以向 acts_like?(:some_class) 的调用者发出其兼容性信号。

例如,Active Support 扩展 Date 以定义 acts_like_date? 方法,并扩展 Time 以定义 acts_like_time?。因此,开发人员可以调用 x.acts_like?(:time)x.acts_like?(:date) 来测试鸭子类型兼容性,而能够像 Time 一样工作的类也可以定义一个 acts_like_time? 方法来进行互操作。

请注意,标记方法只应存在。它不会被调用,因此其主体或返回值无关紧要。

示例:一个提供与 String 相同接口的类

此类可以定义

class Stringish
  def acts_like_string?
  end
end

然后,客户端代码可以通过这种方式查询鸭子类型安全性

Stringish.new.acts_like?(:string) # => true
# File activesupport/lib/active_support/core_ext/object/acts_like.rb, line 33
def acts_like?(duck)
  case duck
  when :time
    respond_to? :acts_like_time?
  when :date
    respond_to? :acts_like_date?
  when :string
    respond_to? :acts_like_string?
  else
    respond_to? :"acts_like_#{duck}?"
  end
end

blank?()

如果一个对象为假、空或一个空格字符串,则该对象为空。例如,nil、”、‘ ’、[]、{} 和 false 均为空。

这简化了

!address || address.empty?

address.blank?

@return [true, false]

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 18
def blank?
  respond_to?(:empty?) ? !!empty? : !self
end

deep_dup()

如果对象可复制,则返回该对象的深层副本。如果不可复制,则返回 self

object = Object.new
dup    = object.deep_dup
dup.instance_variable_set(:@a, 1)

object.instance_variable_defined?(:@a) # => false
dup.instance_variable_defined?(:@a)    # => true
# File activesupport/lib/active_support/core_ext/object/deep_dup.rb, line 15
def deep_dup
  duplicable? ? dup : self
end

duplicable?()

你能安全地复制此对象吗?

对于方法对象为假;否则为真。

# File activesupport/lib/active_support/core_ext/object/duplicable.rb, line 26
def duplicable?
  true
end

html_safe?()

# File activesupport/lib/active_support/core_ext/string/output_safety.rb, line 7
def html_safe?
  false
end

in?(another_object)

如果此对象包含在参数中,则返回 true。

当参数为 Range 时,#cover? 用于正确处理开放范围内的包含检查。否则,参数必须是任何响应 #include? 的对象。用法

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

对于非Range参数,如果参数未响应#include?,则会抛出ArgumentError

# File activesupport/lib/active_support/core_ext/object/inclusion.rb, line 15
def in?(another_object)
  case another_object
  when Range
    another_object.cover?(self)
  else
    another_object.include?(self)
  end
rescue NoMethodError
  raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
end

instance_values()

返回一个哈希,其中字符串键将不带“@”的实例变量名称映射到其对应的值。

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

C.new(0, 1).instance_values # => {"x" => 0, "y" => 1}
# File activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 14
def instance_values
  instance_variables.to_h do |ivar|
    [ivar[1..-1].freeze, instance_variable_get(ivar)]
  end
end

instance_variable_names()

返回一个实例变量名称数组,其中包括“@”的字符串。

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

C.new(0, 1).instance_variable_names # => ["@y", "@x"]
# File activesupport/lib/active_support/core_ext/object/instance_variables.rb, line 30
def instance_variable_names
  instance_variables.map(&:name)
end

presence()

如果接收器存在,则返回接收器,否则返回nilobject.presence等同于

object.present? ? object : nil

例如,类似于

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

变为

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

@return [Object]

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 45
def presence
  self if present?
end

presence_in(another_object)

如果接收器包含在参数中,则返回接收器,否则返回nil。参数必须是可以响应#include?的任何对象。用法

params[:bucket_type].presence_in %w( project calendar )

如果参数未响应#include?,则会抛出ArgumentError

@return [Object]

# File activesupport/lib/active_support/core_ext/object/inclusion.rb, line 34
def presence_in(another_object)
  in?(another_object) ? self : nil
end

present?()

如果对象不为空,则该对象存在。

@return [true, false]

# File activesupport/lib/active_support/core_ext/object/blank.rb, line 25
def present?
  !blank?
end

to_param()

to_s 的别名。

# File activesupport/lib/active_support/core_ext/object/to_query.rb, line 7
def to_param
  to_s
end

to_query(key)

将对象转换为适合用作 URL 查询字符串的字符串,使用给定的 key 作为参数名称。

# File activesupport/lib/active_support/core_ext/object/to_query.rb, line 13
def to_query(key)
  "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
end

try(*args, &block)

调用名称作为第一个参数的公共方法,就像 public_send 所做的那样,但如果接收器没有响应,则调用返回 nil 而不是引发异常。

定义此方法是为了能够编写

@person.try(:name)

而不是

@person.name if @person

try 调用可以链接

@person.try(:spouse).try(:name)

而不是

@person.spouse.name if @person && @person.spouse

如果接收器没有响应方法,try 也将返回 nil

@person.try(:non_existing_method) # => nil

而不是

@person.non_existing_method if @person.respond_to?(:non_existing_method) # => nil

无论接收器是否响应方法,当在 nil 上调用时,try 返回 nil

nil.try(:to_i) # => nil, rather than 0

如果调用,则参数和块将转发到方法

@posts.try(:each_slice, 2) do |a, b|
  ...
end

签名中的参数数量必须匹配。如果对象响应方法,则尝试调用,并且在参数不匹配的情况下仍然引发 ArgumentError

如果在没有参数的情况下调用 try,则它会将接收器传递给给定的块,除非它是 nil

@person.try do |p|
  ...
end

您还可以调用 try 并带有一个块而不接受参数,并且将对该块进行 instance_eval

@person.try { upcase.truncate(50) }

另请注意,tryObject 上定义。因此,它不适用于没有 Object 作为其祖先的类的实例,例如 BasicObject 的直接子类。

# File activesupport/lib/active_support/core_ext/object/try.rb, line 39
  

try!(*args, &block)

try相同,但如果接收者不是nil且未实现尝试的方法,则会引发NoMethodError异常。

"a".try!(:upcase) # => "A"
nil.try!(:upcase) # => nil
123.try!(:upcase) # => NoMethodError: undefined method `upcase' for 123:Integer
# File activesupport/lib/active_support/core_ext/object/try.rb, line 104

with(**attributes)

在块周围设置和恢复公共属性。

client.timeout # => 5
client.with(timeout: 1) do
  client.timeout # => 1
end
client.timeout # => 5

此方法是常见 begin/ensure 模式的简写

old_value = object.attribute
begin
  object.attribute = new_value
  # do things
ensure
  object.attribute = old_value
end

只要读取器和写入器方法都是公共的,就可以在任何对象上使用它。

# File activesupport/lib/active_support/core_ext/object/with.rb, line 24
def with(**attributes)
  old_values = {}
  begin
    attributes.each do |key, value|
      old_values[key] = public_send(key)
      public_send("#{key}=", value)
    end
    yield
  ensure
    old_values.each do |key, old_value|
      public_send("#{key}=", old_value)
    end
  end
end

with_options(options, &block)

一种优雅的方式,可以将重复项从传递给一系列方法调用的选项中剔除。块中调用的每个方法(块变量作为接收者)都会将其选项与提供的默认options Hash或类似Hash的对象合并。在块变量上调用的每个方法都必须将选项哈希作为其最终参数。

如果没有with_options,此代码包含重复项

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,我们可以删除重复项

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

它还可以与显式接收者一起使用

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

当您不传递显式接收者时,它会在合并选项上下文中执行整个块

class Account < ActiveRecord::Base
  with_options dependent: :destroy do
    has_many :customers
    has_many :products
    has_many :invoices
    has_many :expenses
  end
end

with_options还可以嵌套,因为调用会转发给其接收者。

注意:每个嵌套级别除了自己的默认值外,还会合并继承的默认值。

class Post < ActiveRecord::Base
  with_options if: :persisted?, length: { minimum: 50 } do
    validates :content, if: -> { content.present? }
  end
end

该代码等同于

validates :content, length: { minimum: 50 }, if: -> { content.present? }

因此,将忽略if键的继承默认值。

注意:您不能在with_options内部隐式调用类方法。您可以使用类名来访问这些方法

class Phone < ActiveRecord::Base
  enum :phone_number_type, { home: 0, office: 1, mobile: 2 }

  with_options presence: true do
    validates :phone_number_type, inclusion: { in: Phone.phone_number_types.keys }
  end
end

当省略块参数时,将返回装饰的Object实例

module MyStyledHelpers
  def styled
    with_options style: "color: red;"
  end
end

styled.link_to "I'm red", "/"
# => <a href="/" style="color: red;">I'm red</a>

styled.button_tag "I'm red too!"
# => <button style="color: red;">I'm red too!</button>
# File activesupport/lib/active_support/core_ext/object/with_options.rb, line 92
def with_options(options, &block)
  option_merger = ActiveSupport::OptionMerger.new(self, options)

  if block
    block.arity.zero? ? option_merger.instance_eval(&block) : block.call(option_merger)
  else
    option_merger
  end
end