Skip to content

Instantly share code, notes, and snippets.

View tdg5's full-sized avatar

Danny Guinther tdg5

View GitHub Profile
@tdg5
tdg5 / itoa.rb
Last active August 29, 2015 14:17
itoa method
def itoa(int, base = 10)
str = ""
while int > 0
str = ((int % base) + 48).chr + str
int /= base
end
str
end
def itoarr(int, base = 10)
@tdg5
tdg5 / factorials.rb
Created March 20, 2015 18:39
4 kinds of factorial
require "benchmark/ips"
def recursive_factorial(n)
n < 2 ? 1 : n * recursive_factorial(n - 1)
end
def tail_recursive_factorial(n, accumulator = 1)
n < 2 ? accumulator : tail_recursive_factorial(n - 1, n * accumulator)
end
@tdg5
tdg5 / stack_size_introspection.rb
Last active August 29, 2015 14:17
PoC of detecting tail call optimization by monitoring stack size via a lambda callback
require 'tco_method'
module StackBuster
def self.fact_yielder(n, acc = 1, &block)
block.call(acc)
n <= 1 ? acc : fact_yielder(n - 1, n * acc, &block)
end
end
puts "StackBuster:"
@tdg5
tdg5 / eagerly_losing_logic.rb
Created March 1, 2015 03:07
Errors during eager evaluation cause result of logical expression to be lost
or_result = nil
begin
or_result = true | Seriously(this(is(valid(Ruby!))))
rescue NameError
puts "NameError :("
end
# Name Error :(
or_result
# => nil
@tdg5
tdg5 / prime_finder.rb
Last active August 29, 2015 14:16
Sieve generated prime finder
class PrimeFinder
attr_reader :limit, :primes
def initialize(limit)
@limit = limit
@primes = []
generate_primes!(limit)
end
def prime?(candidate)
@tdg5
tdg5 / tagged_console.rb
Created February 25, 2015 18:16
alternative means of adding tagging to rails console comand
# Add to require from script/rails before rails/commands is required.
if /^c(?:onsole)?$/ === ARGV[0] && index = ARGV.index { |arg| /-(t|-tag)/ === arg }
# Intercept the arguments vector before it is passed to Rails::Console.
# Drop the -t or --tag arg and following argument which should be the tag
# itself. That's all that should need to be done because the tag will show up
# in the command name given how the command was invoked.
ARGV.slice!(index, 2)
end
@tdg5
tdg5 / pe_08_with_simple.rb
Created February 18, 2015 11:19
Project Euler #08 with simpler alternative
# Project Euler #8 - Largest product in a series
# https://projecteuler.net/problem=8
#
# Find the thirteen adjacent digits in the 1000-digit number that have the
# greatest product. What is the value of this product?
def largest_product_in_series(series, adjacency_length = 13)
factors = []
largest_product = 0
current_product = 1
@tdg5
tdg5 / string_split_benchmark.rb
Last active August 29, 2015 14:15
String#split is much, much faster without any arguments
require 'benchmark/ips'
number = "731671765313306249192251196744265747423553491949349698352031277450632623957831801698480186947885184385861560789112949495459501737958331952853208805511125406987471585238630507156932909632952274430435576689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360082325753042
@tdg5
tdg5 / lexical_scope_is_weird.rb
Created January 15, 2015 20:52
Lexical Scope Is Weird
module LexicalScope
SCOPED_CONSTANT = [:lexical_scope].freeze
def scoped_value
value = defined?(super) ? super.dup : []
value.concat(SCOPED_CONSTANT)
end
end
module IsWeird
@tdg5
tdg5 / mocha_calls_through.rb
Last active January 11, 2016 15:43
Mocha Expectation#calls_through
module Concerns
module Mocha
module CallsThrough
def calls_through
method_name = @method_matcher.expected_method_name
@mock.instance_variable_get(:@receiver)
@original_method = @mock.instance_variable_get(:@mockery).stubba.stubba_methods.find do |mockery|
mockery.method == method_name
end.instance_variable_get(:@original_method)
self