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
require 'stringio' | |
def capture | |
old_stdout, $stdout = $stdout, StringIO.new | |
fake_stdout = $stdout | |
yield | |
$stdout = old_stdout | |
fake_stdout.string | |
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
module Mushroom | |
@color = :blue | |
def self.color | |
@color | |
end | |
def self.color=(this_color) | |
@color = this_color | |
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 Integer | |
def collatz_chain_size | |
n = self | |
chain = [] | |
until n == 1 | |
chain << n | |
n.even? ? n /= 2 : n = n * 3 + 1 | |
end | |
[chain.size, chain.first] | |
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
# http://users.minet.uni-jena.de/~dittrich//p/Dit2005upp.pdf | |
# https://github.com/videlalvaro/erlang-prime-sieve | |
require 'mathn' | |
module ChemicalPrime | |
def self.take n | |
sequence(n).map { |n| n * 2 + 1 }.unshift 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
def blink string, duration: 4, interval: 0.5 | |
start = Time.now | |
while Time.now < start + duration | |
print string | |
sleep interval | |
[?\r, ?\s, ?\r].each { |char| print char * string.size } | |
sleep interval | |
end | |
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
module ArityRange | |
def arity_range | |
args = parameters.map &:first | |
req = args.count :req | |
opt = args.include?(:rest) ? Float::INFINITY : args.count(:opt) | |
keyreq = args.count :keyreq | |
keyopt = args.include?(:keyrest) ? Float::INFINITY : args.count(:key) |
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
module Enumerable | |
def peek_at index | |
value, _ = each_with_index.select { |_, i| index == i }.first | |
value | |
end | |
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
module Iterator | |
def self.new element, increment = 1 | |
Enumerator.new do |iteration| | |
loop do | |
iteration << element | |
increment.abs.times do | |
element = if increment >= 0 | |
element.next if element.respond_to? :next | |
else | |
if element.respond_to? :pred |
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 Bdays | |
attr_reader :birthdates | |
def initialize n | |
@birthdates = Array.new(n) { [*0..365].sample }.map do |s| | |
birthday = Time.new(0) + s * 60 * 60 * 24 | |
[birthday.month, birthday.day] | |
end | |
shared_birthdates |
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
module ArrayIteration | |
def next | |
@index ||= 0 | |
raise StopIteration, 'iteration reached an end' if @index.next > size | |
@index += 1 | |
self[@index.pred] | |
end | |
def pred | |
@index ||= 0 |
OlderNewer