This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bcrypt' | |
class User | |
attr_reader :email, :password | |
def initialize attributes={} | |
@email = attributes[:email] | |
@password = BCrypt::Password.create(attributes[:password]).to_s | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bigdecimal' | |
class Fixnum | |
def / other | |
BigDecimal.new(self) / other | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
class C | |
def m | |
end | |
end | |
class D | |
def m | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
class FooProxy | |
attr_reader :foo | |
def initialize(foo) | |
@foo = foo | |
end | |
def bar |