跳至内容 跳至搜索
方法
B
T

实例公共方法

blank?()

nil 为 blank

nil.blank? # => true

@return [true]

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

to_param()

返回 self

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

to_query(key)

返回经过 CGI 转义的 key

# File activesupport/lib/active_support/core_ext/object/to_query.rb, line 21
def to_query(key)
  CGI.escape(key.to_param)
end

try(*, &)

调用 try 方法在 nil 对象上始终返回 nil。当在可能返回 nil 的关联中进行导航时,它会特别有用。

nil.try(:name) # => nil

未使用 try

@person && @person.children.any? && @person.children.first.name

使用 try

@person.try(:children).try(:first).try(:name)
# File activesupport/lib/active_support/core_ext/object/try.rb, line 148
def try(*, &)
  nil
end

try!(*, &)

调用 try! 方法在 nil 对象上始终返回 nil

nil.try!(:name) # => nil
# File activesupport/lib/active_support/core_ext/object/try.rb, line 155
def try!(*, &)
  nil
end