提交 d31178fb 编写于 作者: R Rafael França 提交者: GitHub

Merge pull request #28145 from treiff/update-docs-ActiveRecord__Result

Improve docs for ActiveRecord::Result. [ci skip]
...@@ -41,10 +41,15 @@ def initialize(columns, rows, column_types = {}) ...@@ -41,10 +41,15 @@ def initialize(columns, rows, column_types = {})
@column_types = column_types @column_types = column_types
end end
# Returns the number of elements in the rows array.
def length def length
@rows.length @rows.length
end end
# Calls the given block once for each element in row collection, passing
# row as parameter.
#
# Returns an +Enumerator+ if no block is given.
def each def each
if block_given? if block_given?
hash_rows.each { |row| yield row } hash_rows.each { |row| yield row }
...@@ -53,6 +58,7 @@ def each ...@@ -53,6 +58,7 @@ def each
end end
end end
# Returns an array of hashes representing each row record.
def to_hash def to_hash
hash_rows hash_rows
end end
...@@ -60,11 +66,12 @@ def to_hash ...@@ -60,11 +66,12 @@ def to_hash
alias :map! :map alias :map! :map
alias :collect! :map alias :collect! :map
# Returns true if there are no records. # Returns true if there are no records, otherwise false.
def empty? def empty?
rows.empty? rows.empty?
end end
# Returns an array of hashes representing each row record.
def to_ary def to_ary
hash_rows hash_rows
end end
...@@ -73,11 +80,15 @@ def [](idx) ...@@ -73,11 +80,15 @@ def [](idx)
hash_rows[idx] hash_rows[idx]
end end
# Returns the first record from the rows collection.
# If the rows collection is empty, returns +nil+.
def first def first
return nil if @rows.empty? return nil if @rows.empty?
Hash[@columns.zip(@rows.first)] Hash[@columns.zip(@rows.first)]
end end
# Returns the last record from the rows collection.
# If the rows collection is empty, returns +nil+.
def last def last
return nil if @rows.empty? return nil if @rows.empty?
Hash[@columns.zip(@rows.last)] Hash[@columns.zip(@rows.last)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册