Skip to content

Instantly share code, notes, and snippets.

View jgaskins's full-sized avatar

Jamie Gaskins jgaskins

View GitHub Profile
@jgaskins
jgaskins / luhn_checksum.rb
Created March 6, 2013 20:37
Ruby implementation of the Luhn checksum algorithm
# Luhn Checksum
#
# http://en.wikipedia.org/wiki/Luhn_algorithm
#
# Implement a Ruby program that outputs the validity of a number by applying the Luhn Checksum.
#
# Input:
# The program should accept a single number to validate.
#
# Output:
@jgaskins
jgaskins / user.rb
Created April 9, 2013 17:31
Here's my password, so hash it maybe?
require 'bcrypt'
class User
attr_reader :email, :password
def initialize attributes={}
@email = attributes[:email]
@password = BCrypt::Password.create(attributes[:password]).to_s
end
end
@jgaskins
jgaskins / hashed_password.rb
Created April 9, 2013 17:55
Hashing Passwords with Virtus as a Service
require 'bcrypt'
class HashedPassword < Virtus::Attribute::String
class Hashed < Virtus::Attribute::Writer::Coercible
def coerce(value)
BCrypt::Password.create(value)
end
end
lazy true # No idea what this does. I found it in spec/integration/custom_attributes_spec.rb
@jgaskins
jgaskins / bigdecimal_division.rb
Created April 10, 2013 15:57
Force integer division to BigDecimal
require 'bigdecimal'
class Fixnum
def / other
BigDecimal.new(self) / other
end
end
@jgaskins
jgaskins / redefine_constant.rb
Last active December 16, 2015 02:19
Temporarily reassigning a constant
module A
module B
C = Class.new
end
end
def redefine_constant full_const_name, value
const_path = full_const_name.to_s.split('::')
namespace = const_path[0..-2].inject(Kernel) { |scope, name| scope.const_get(name) }
const = const_path[-1]
@jgaskins
jgaskins / benchmark_method_cache.rb
Created April 11, 2013 17:25
Effects of Module#extend busting method caches on MRI 1.9.3, Rubinius and JRuby.
require 'benchmark'
class C
def m
end
end
class D
def m
end
@jgaskins
jgaskins / fibonacci.rb
Last active December 16, 2015 22:39
Demonstrating recursive Fibonacci calculation visits each node in the sequence the number of times corresponding to its position in the calculation. So fib[n] == fib.calls[max_calculated - n].
class Fibonacci
attr_reader :fibs, :calls
def initialize
@fibs = [0, 1]
@calls = Hash.new { |h, k| h[k] = 0 }
end
def [] position
calls[position] += 1
@jgaskins
jgaskins / constantize.rb
Created June 9, 2013 16:23
Much leaner version of ActiveSupport's String#constantize
class String
def constantize
split('::').inject(Kernel, &:const_get)
end
end
A, A::B = Module.new, Class.new
p "A::B".constantize # => A::B
p "Object".constantize # => Object
@jgaskins
jgaskins / prime_detector.rb
Created June 9, 2013 21:56
Optimizing prime-number detection by short-circuiting when a zero remainder is found
require 'benchmark'
require 'prime'
class Integer
def simple_prime?
(2..Math.sqrt(self).floor).all? { |i| (self % i).nonzero? }
end
def clever_prime?
return true if self == 2
@jgaskins
jgaskins / object_proxy.rb
Last active December 18, 2015 14:49
Proxy object
require 'benchmark'
class FooProxy
attr_reader :foo
def initialize(foo)
@foo = foo
end
def bar