This file contains 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
# 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 |
This file contains 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
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 |
This file contains 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
# 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 |
This file contains 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 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 | |
This file contains 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 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. |
This file contains 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
=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. |
This file contains 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
# 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 } |
This file contains 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
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) |
This file contains 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
# 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. |
This file contains 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
# 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}" }; |