Skip to content

Instantly share code, notes, and snippets.

@ericgj
Created January 30, 2011 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericgj/802961 to your computer and use it in GitHub Desktop.
Save ericgj/802961 to your computer and use it in GitHub Desktop.
A non-destructive implementation of Table#select_columns that creates a new table (note this is not tested)
class Table
def max_y
@rows[0].length
end
def select_columns
selected = (0..(max_y - 1)).map do |i|
col = @rows.map {|row| row[i] }
col if yield(col)
end.compact.transpose
if @header_support
self.class.new [@headers] << selected, :headers => true
else
self.class.new selected, :headers => false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment