Method: Sequel::Dataset#map

Defined in:
lib/sequel/dataset/actions.rb

#map(column = nil, &block) ⇒ Object

Maps column values for each record in the dataset (if an argument is given) or performs the stock mapping functionality of Enumerable otherwise. Raises an Error if both an argument and block are given.

DB[:table].map(:id) # SELECT * FROM table
# => [1, 2, 3, ...]

DB[:table].map{|r| r[:id] * 2} # SELECT * FROM table
# => [2, 4, 6, ...]

You can also provide an array of column names:

DB[:table].map([:id, :name]) # SELECT * FROM table
# => [[1, 'A'], [2, 'B'], [3, 'C'], ...]

464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/sequel/dataset/actions.rb', line 464

def map(column=nil, &block)
  if column
    raise(Error, 'Must call Dataset#map with either an argument or a block, not both') if block
    return naked.map(column) if row_proc
    if column.is_a?(Array)
      super(){|r| r.values_at(*column)}
    else
      super(){|r| r[column]}
    end
  else
    super(&block)
  end
end