Skip to content

Instantly share code, notes, and snippets.

@HammerInTheNews
HammerInTheNews / match_and_print.rb
Created December 13, 2013 20:29
Read all the lines of a text file into an array if they match a certain pattern.
lines = File.new('foo.txt', 'r').select do |line|
line =~ /pattern/
end
@HammerInTheNews
HammerInTheNews / CSV_generate.rb
Created December 13, 2013 20:32
Generate a CSV file – write rows to csvout.csv, read and print
require 'csv'
outfile = File.open('csvout.csv', 'wb')
CSV::Writer.generate(outfile) do |csv|
csv << ['c1', nil, 5]
csv << ['c2', 1, 2, 'abc']
end
outfile.close
#also read the file and print
@HammerInTheNews
HammerInTheNews / Duplicates_Removed.rb
Created December 13, 2013 20:33
Remove Duplicates from arrays
dups = [8,1,2,1,4,4,5,7,8,5]
trimmed = dups.uniq
@HammerInTheNews
HammerInTheNews / strip_each_line.rb
Created December 13, 2013 20:29
Read all the lines of a text file into a list with some simple processing per line.
lines = File.new('foo.txt', 'r').collect() do |line|
line.strip()
end