Skip to content

Instantly share code, notes, and snippets.

@tdeo
tdeo / memoize.rb
Last active July 6, 2018 09:24
Ruby memoization made easy
# Memoization made easy. Use it the following way
#
# Memoize all calls
# memoize def my_function(n)
# ...
# end
#
# Memoize only a ratio of calls (5% here):
# memoize 0.05 def my_function(n)
# ...
@tdeo
tdeo / Mastermind.rb
Created July 20, 2018 20:16
A command-line mastermind game
class Mastermind
COLOURS = 'A'.upto('Z').to_a
def initialize(colour_count = 6, size = 4)
fail "Size must be <= #{COLOURS.size}" unless size <= COLOURS.size
@size = size
@colours = COLOURS.first(colour_count)
@code = size.times.map { @colours.sample }
@moves = 0
puts "Guess the code, size #{@size}, letters are (#{@colours.join(', ')})"