Skip to content

Instantly share code, notes, and snippets.

@jongillies
Created February 27, 2015 22:01
Show Gist options
  • Save jongillies/651464b395ab8830610a to your computer and use it in GitHub Desktop.
Save jongillies/651464b395ab8830610a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Payment = Struct.new(:payee, :amount, :tags)
Dateline = Struct.new(:date)
TRANSACTIONS = [
Dateline.new("2015-01-01"),
Payment.new("Milliways", 25476, "dining travel"),
Payment.new("Sirius Cybernetics", 7839),
Payment.new("Old Pink Dog Bar", 2790, "dining"),
Dateline.new("2015-01-02"),
Payment.new("Magrathea", 49900000000000000, "real-estate"),
Payment.new("Big Bang Burger Bar", 4780),
Payment.new("Megadodo", 7850, "travel"),
]
# So you have an array of objects that contain 2 different object types.
# So you want just the payments?
puts TRANSACTIONS.grep(Payment)
# Think about that for a second...
# You did not pass grep a string, you passed in an struct object class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment