Skip to content

Instantly share code, notes, and snippets.

import glob
import json
import os
for f in glob.glob('*/*.json'):
outlist = []
doc = json.load(open(f))
for app in doc:
out_stub = {}
out_stub['title'] = app['title']

Luhn Checksum

Implement a Ruby program that outputs the validity of a number by applying the Luhn Checksum.

http://en.wikipedia.org/wiki/Luhn_algorithm

Please treat this as you would any other task you'd encounter during your daily work. This will show us how currently code, and not how you coded 6, 12 or 18 months ago. Your code should also be suitably tested. Also, please do not feel that you need to spend a great deal of time working on a solution.

There are many solutions and code snippets online to do a Luhn Checksum. Please resist using those.

Keybase proof

I hereby claim:

  • I am tomharris on github.
  • I am tomharris (https://keybase.io/tomharris) on keybase.
  • I have a public key whose fingerprint is 4EE9 2C56 96E3 3A36 82F0 F7C4 3DE7 4993 F0A5 47A9

To claim this, I am signing this object:

class Thing < ActiveRecord::Base
def self.sort_by_column(column, direction)
if column_names.include?(column) and %w(asc desc).include?(direction)
order("#{column} #{direction}")
else
self
end
end
end
@tomharris
tomharris / data_load.rb
Created February 25, 2013 23:37
Checkout db connections from the pool with some friendly syntax.
module DataLoad
class Thing1
include DatabaseConnectionHelper
def reload_all
with_master_connection do |connection|
sql = <<-SQL
create temporary table tmp_thing1 (
-- ...
)
@tomharris
tomharris / data_load.rb
Created February 25, 2013 23:27
The connection is not properly checked-out of the connection pool. AR may give it to other threads to use
module DataLoad
class Thing1
def reload_all
connection = ActiveRecord::Base.connection
sql = <<-SQL
create temporary table tmp_thing1 (
-- ...
)
@tomharris
tomharris / upload_file.rb
Created February 25, 2013 23:06
Example of the usage of the non thread-safe aws-s3 gem.
def upload
# ...
AWS::S3::Base.establish_connection!(
:access_key_id => aws_access_key_id,
:secret_access_key => aws_secret_access_key
)
File.open(file_path) do |file|
Date::DATE_FORMATS[:custom_format] = '%m/%d/%Y'
# ...
user.created_at.to_s(:custom_format)
@tomharris
tomharris / gist:4570061
Created January 19, 2013 01:07
Disable ActiveRecord callbacks
class SomeModel
after_save :queue_a_task, unless: lambda { !!@callbacks_disabled }
def do_something_without_queuing_a_task
disable_callbacks!
do_something_that_triggers_callbacks
enable_callbacks!
end
private
class WordDictionary
def initialize(dictionary_filename = '/usr/share/dict/words')
@words = File.readlines(dictionary_filename)
@words = @words.collect { |word| word.strip } # File.readlines preserves the newlines at the end of the line
end
def words_with_five_vowels_in_ascending_order
@words.select { |word| word.count('aeiou') == 5 && word =~ /.*?[aA].*?[eE].*?[iI].*?[oO].*?[uU].*?/ }
end