Skip to content

Instantly share code, notes, and snippets.

View justinko's full-sized avatar

Justin Ko justinko

View GitHub Profile
require 'rubygems'
require 'sequel'
require 'csv'
def parse_data(file_name)
CSV.read(file_name, headers: :first_row).to_a
end
db = Sequel.sqlite
Merchant Date Ends Deal Price Value
Burger King 10/2/2011 10/4/2011 Your way 25 50
McDonalds 10/5/2011 Not really food 22 44
Arbys 10/8/2011 10/10/2011 More burgers 7 14
@justinko
justinko / daily_planet_export.txt
Created March 10, 2013 01:36
Example of Ruby's StringScanner class.
Merchant Date Ends Deal Price Value
Burger King 10/2/2011 10/4/2011 Your way 25 50
McDonalds 10/5/2011 Not really food 22 44
Arbys 10/8/2011 10/10/2011 More burgers 7 14
@justinko
justinko / cloning.rb
Created March 5, 2013 20:38
You need to "clone" an Active Record object. Which one of the examples do you prefer (or comment with your own!) and why?
module Cloning
def clone_record(column: :name)
self.class.create(column => cloned_column_attribute(column))
end
private
def cloned_column_attribute(column)
1.upto(Float::INFINITY) do |clone_number|
column_attribute = public_send(column)
class AppCache
def initialize
@store = Store.new
end
def cache(*keys, &block)
options = keys.extract_options!
@store.cache(keys, options[:expires_in], &block)
end
1.upto(100) do |i|
puts ''.tap {|output|
output << 'Fizz' if i.modulo(3).zero?
output << 'Buzz' if i.modulo(5).zero?
output << i.to_s if output.empty?
}
end
class SyncToAnalyticsService
ConnectionFailure = Class.new(StandardError)
def self.perform(data)
new(data).perform
end
def initialize(data)
@data = data.symbolize_keys
@account = Account.find(@data[:account_id])
class SyncToAnalyticsService
ConnectionFailure = Class.new(StandardError)
def self.perform(data)
new(data).perform
end
def initialize(data)
@data = data.symbolize_keys
@account = Account.find(@data[:account_id])
@justinko
justinko / my_spec.rb
Created November 8, 2012 19:52
Do not make `describe` global
describe
class Post
FetchError = Class.new(StandardError)
def self.able_to_fetch?
# https://github.com/djberg96/net-ping
# `ping?` determines its boolean by rescuing an exception (SocketError).
# Where that exception is handled is up to you. Do you want it to occur in
# a gem (net-ping) or in your controller? Either way, doesn't fucking matter,
# someone's gotta do it.
Net::Ping::HTTP.new(service_url).ping?