Created
February 28, 2015 17:10
-
-
Save mmmries/b33ce16eba7c24b2ef6b to your computer and use it in GitHub Desktop.
Clever Ruby Tricks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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