Skip to content

Instantly share code, notes, and snippets.

@mmmries
Created February 28, 2015 17:10
Show Gist options
  • Save mmmries/b33ce16eba7c24b2ef6b to your computer and use it in GitHub Desktop.
Save mmmries/b33ce16eba7c24b2ef6b to your computer and use it in GitHub Desktop.
Clever Ruby Tricks
# Shortcut for Dir Globs
Dir["data_sets/*.csv"].each do |filepath|
process_data_set(filepath)
end
# ARGF an IO object that wraps around all commandline paths
ARGF.readlines.select do |line|
line.start_with?("A")
end.each do |line|
puts line
end
# inject on a hash to get the key and value separate
attributes = {:id => :integer, :amount => :float}
attirbutes.inject([]) do |list, pair| #normally you just get the object being reduced into and a key/value pair
key, value = pair
# do something
end
attirbutes.inject([]) do |list, (key, value)| #you can get them separately like this
# do something
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment