Skip to content

Instantly share code, notes, and snippets.

@Fivell
Forked from johana-star/rocky_raccoon.csv
Created January 14, 2017 22:24
Show Gist options
  • Save Fivell/ac658d010c6c05605ee807577125e998 to your computer and use it in GitHub Desktop.
Save Fivell/ac658d010c6c05605ee807577125e998 to your computer and use it in GitHub Desktop.
Ruby Riddle: Why doesn't to_proc work on a refined Hash?
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 12 columns, instead of 4. in line 1.
Task ID,Type,Name,Status,Project,Context,Start Date,Due Date,Completion Date,Duration,Flagged,Notes
1,Context,Rocky Raccoon,active
1.1,Action,Now somewhere in the black mountain hills of Dakota,,,Rocky Raccoon,,,,,0,
1.2,Action,There lived a young boy named Rocky Raccoon,,,Rocky Raccoon,,,,,0,
1.3,Action,And one day his woman ran off with another guy,,,Rocky Raccoon,,,,,0,
1.4,Action,He hit young Rocky in the eye,,,Rocky Raccoon,,,,,0,
1.5,Action,Rocky didn't like that he said I'm gonna get that boy,,,Rocky Raccoon,,,,,0,
1.6,Action,So one day he walked into town,,,Rocky Raccoon,,,,,0,
1.7,Action,Booked himself a room in the local saloon,,,Rocky Raccoon,,,,,0,
1.8,Action,Rocky Raccoon checked into his room,,,Rocky Raccoon,,,,,0,
1.9,Action,Only to find Gideon's bible,,,Rocky Raccoon,,,,,0,
1.10,Action,Rocky had come equipped with a gun,,,Rocky Raccoon,,,,,0,
1.11,Action,To shoot off the legs of his rival,,,Rocky Raccoon,,,,,0,
1.12,Action,His rival it seems had broken his dreams,,,Rocky Raccoon,,,,,0,
1.13,Action,By stealing the girl of his fancy,,,Rocky Raccoon,,,,,0,
1.14,Action,Her name was Magill and she called herself Lil,,,Rocky Raccoon,,,,,0,
1.15,Action,But everyone knew her as Nancy,,,Rocky Raccoon,,,,,0,
1.16,Action,"Now she and her man, who called himself Dan",,,Rocky Raccoon,,,,,0,
1.17,Action,Were in the next room at the hoedown,,,Rocky Raccoon,,,,,0,
1.18,Action,Rocky burst in and grinning a grin,,,Rocky Raccoon,,,,,0,
1.19,Action,"He said, Danny boy this is a showdown",,,Rocky Raccoon,,,,,0,
1.20,Action,But Daniel was hot he drew first and shot,,,Rocky Raccoon,,,,,0,
1.21,Action,And Rocky collapsed in the corner,,,Rocky Raccoon,,,,,0,
1.22,Action,Now the doctor came in stinking of gin,,,Rocky Raccoon,,,,,0,
1.23,Action,And proceeded to lie on the table,,,Rocky Raccoon,,,,,0,
1.24,Action,"He said, Rocky you met your match",,,Rocky Raccoon,,,,,0,
1.25,Action,"And Rocky said, Doc it's only a scratch",,,Rocky Raccoon,,,,,0,
1.26,Action,"And I'll be better, I'll be better Doc as soon as I am able",,,Rocky Raccoon,,,,,0,
1.27,Action,And now Rocky Raccoon he fell back in his room,,,Rocky Raccoon,,,,,0,
1.28,Action,Only to find Gideon's bible,,,Rocky Raccoon,,,,,0,
1.29,Action,Gideon checked out and he left it no doubt,,,Rocky Raccoon,,,,,0,
1.30,Action,To help with good Rocky's revival,,,Rocky Raccoon,,,,,0,
require "csv"
module HashRefinements
refine Hash do
def is_action?
self[:type] == "Action"
end
end
end
class OmnifocusImporter
using HashRefinements
def initialize(file)
CSV::Converters[:blank_to_nil] = lambda do |field|
field && field.empty? ? nil : field
end
@csv = CSV.new(
IO.read(file),
headers: true,
header_converters: :symbol,
converters: [:all, :blank_to_nil]
).to_a
end
def import
@csv.map(&:to_hash).
select(&:is_action?)
# The line above throws an error when this is run.
end
end
OmnifocusImporter.new("./rocky_raccoon.csv").import
require "csv"
module HashRefinements
refine Hash do
def is_action?
self[:type] == "Action"
end
end
end
class OmnifocusImporter
using HashRefinements
def initialize(file)
CSV::Converters[:blank_to_nil] = lambda do |field|
field && field.empty? ? nil : field
end
@csv = CSV.new(
IO.read(file),
headers: true,
header_converters: :symbol,
converters: [:all, :blank_to_nil]
).to_a
end
def import
@csv.map(&:to_hash).
select { |h| h.is_action? }
# I believe the line above is semantically equivelant
# to throws_excepetion.rb:28, however, it throws no errors.
end
end
OmnifocusImporter.new("./rocky_raccoon.csv").import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment