Skip to content

Instantly share code, notes, and snippets.

View bil-bas's full-sized avatar

Bil Bas bil-bas

View GitHub Profile
@bil-bas
bil-bas / factorial.rb
Created October 22, 2012 22:09
Memoising Integer#factorial in Ruby
# Memoising factorial method.
class Integer
class << self
def factorial_results(number)
raise "number has to be greater than 0" if number < 0
@factorial_results ||= begin
results = Hash.new do |h, k|
h[k] = k * @factorial_results[k - 1]
end
C:/Users/Owner/.pik/rubies/Ruby-193-p286/lib/ruby/gems/1.9.1/bundler/gems/gamebox-b492424483ab/lib/gamebox/core/timer_manager.rb:11:in `add_timer': timer [252818292:shot_recharge] already exists (RuntimeError)
from C:/Users/Owner/RubymineProjects/Other people's junk/foxy/src/behaviors/shooter.rb:86:in `shoot_if_able'
from C:/Users/Owner/RubymineProjects/Other people's junk/foxy/src/behaviors/shooter.rb:21:in `block (3 levels) in <top (required)>'
from C:/Users/Owner/.pik/rubies/Ruby-193-p286/lib/ruby/gems/1.9.1/gems/publisher-1.1.2/lib/publisher.rb:83:in `call'
from C:/Users/Owner/.pik/rubies/Ruby-193-p286/lib/ruby/gems/1.9.1/gems/publisher-1.1.2/lib/publisher.rb:83:in `block in fire'
from C:/Users/Owner/.pik/rubies/Ruby-193-p286/lib/ruby/gems/1.9.1/gems/publisher-1.1.2/lib/publisher.rb:79:in `each'
from C:/Users/Owner/.pik/rubies/Ruby-193-p286/lib/ruby/gems/1.9.1/gems/publisher-1.1.2/lib/publisher.rb:79:in `fire'
from C:/Users/Owner/RubymineProjects/Oth
# List all values in fibonacci series <= n
def fib_up_to(n)
return [] if n < 0 # Idiot-proofing.
vals, succ = [0], 1
while succ <= n
vals << succ
succ = vals[-1] + vals[-2]
end
@bil-bas
bil-bas / attr_accessor_with_history.rb
Created October 14, 2012 16:25
Yah boo, attr accessor with magic and history. Feel free to use it to cheat if you are a king-sized idiot who doesn't want to learn!
class Class
def attr_accessor_with_history(attr_name)
attr_reader attr_name
class_eval %Q{
# Create the attribute's history getter
def #{attr_name}_history
@#{attr_name}_history ||= [nil]
end
@bil-bas
bil-bas / swarley_method_type.rb
Created October 11, 2012 21:37
Swarley's crazy type-checking method generator!
class Class
def define(method_name, argument_types = {}, &block)
raise ArgumentError, "Block required" unless block_given?
raise ArgumentError, "Expected argument Hash" unless argument_types.is_a? Hash
raise ArgumentError, "Expected argument names to be Symbols" unless argument_types.each_key.all? {|x| x.is_a? Symbol }
raise ArgumentError, "Expected argument types to be Classes" unless argument_types.each_value.all? {|x| x.is_a? Class }
wrapper_class = Struct.new *argument_types.keys
# Redirect all other methods to the class, so methods get called there not on the bearer of arguments.
@bil-bas
bil-bas / kevin_encode.rb
Created October 2, 2012 15:08
Encoding and decoding of strings for KevinSjoberg's problem (https://gist.github.com/3818527)
=begin
Problem Solving Challenge #1 (https://gist.github.com/3818527)
Problem solving challenges are fun. They make use of one's problem solving skills and stimulate the programmer's need to challenge themselves.
Here we go
These 1151159511610110199114 numbers represent a word.
The word may only contain a-zA-Z_
The word is of size 8.
In this particular case the secret word was s_ecrets.
@bil-bas
bil-bas / fibonacci_enumerator.rb
Last active September 10, 2016 22:01
Fibonacci sequence - using infinite enumerator in Ruby.
# Enumerable Fibonacci sequence
module Fibonacci
class << self
include Enumerable
def [](index)
raise "index must be >= 0" unless index >= 0
raise "index must be an Integer" unless index.is_a? Integer
find.with_index {|n, i| i == index }
@bil-bas
bil-bas / parser.rb
Created September 9, 2012 17:00 — forked from andrewhl/parser.rb
def parse(page)
table = page.at("table.grid")
rows = table.search("tr").to_a[1..-1]
rows.each.with_object [] do |row, rows|
row = parse_row row.search("td").map(&:text)
rows << row if row
end
end
def parse_row(row)
@bil-bas
bil-bas / wrap_module.rb
Created August 22, 2012 00:46 — forked from drewdeponte/gist:3420790
Require inside module try #1
# code.rb contains:
# class Frog
# end
module MyModule
class_eval File.read("./code.rb"), "code.rb", 1
end
p MyModule::Frog # Will succeed.
p Frog # Will fail.
# List all objects where there are > 1 instance
h = Hash.new(0)
ObjectSpace.each_object {|o| h[o.class] += 1 }
h.select {|k, v| v > 1 }.sort_by {|k, v| -v }.each {|k, v| puts "#{k}: #{v}" };