Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve docs for ActiveRecord::Result. [ci skip] #28145

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion activerecord/lib/active_record/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ def initialize(columns, rows, column_types = {})
@column_types = column_types
end

# Returns the number of elements in the rows array.
def length
@rows.length
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
if block_given?
hash_rows.each { |row| yield row }
Expand All @@ -53,18 +58,20 @@ def each
end
end

# Returns an array of hashes representing each row record.
def to_hash
hash_rows
end

alias :map! :map
alias :collect! :map

# Returns true if there are no records.
# Returns true if there are no records, otherwise false.
def empty?
rows.empty?
end

# Returns an array of hashes representing each row record.
def to_ary
hash_rows
end
Expand All @@ -73,11 +80,15 @@ def [](idx)
hash_rows[idx]
end

# Returns the first record from the rows collection.
# If the rows collection is empty, returns +nil+.
def first
return nil if @rows.empty?
Hash[@columns.zip(@rows.first)]
end

# Returns the last record from the rows collection.
# If the rows collection is empty, returns +nil+.
def last
return nil if @rows.empty?
Hash[@columns.zip(@rows.last)]
Expand Down