Skip to content

Instantly share code, notes, and snippets.

Mark Josef mark

View GitHub Profile
@mark
mark / nonagram_row.rb
Created Aug 15, 2018
Fill in partial row clues for a nonagram row
View nonagram_row.rb
CHAR_TO_CELL = { '_' => :unknown, 'x' => :empty, '#' => :full, '?' => :multiple }
CELL_TO_CHAR = { empty: 'X', full: '#', multiple: '_', unknown: '_' }
def expand_row raw_row
split = raw_row.split /([_X#]\d*)/
items = split.reject { |s| s == '' }
items.map do |item|
item[0] * [1, item[1..-1].to_i].max
end.join.split('').map { |c| CHAR_TO_CELL[c] }
@mark
mark / xkcd-tracery.json
Created Dec 18, 2017
Tracery implementation of XKCD #1930: https://xkcd.com/1930/
View xkcd-tracery.json
{
"origin": ["Did you know that #fact# #what# because of #reason#? Apparently #cause#."],
"fact": [
"the #odd-season# Equinox",
"the #even-season# #even-event#",
"the #timing# #sun-event#",
"Daylight #savings# Time",
"Leap #leap#",
"Easter",
View fiber-enumerable.rb
require 'fiber'
require 'pp'
class MyWrapper
include Enumerable
def initialize
@fiber = fiber
@results = []
end
View gist:d50dfcdb78b2793aff0f2a5de1eb9aba
@mark
mark / def.shard.rb
Last active Aug 31, 2015 — forked from zobar/def.shard.rb
Nested function definitions are a bad idea in Ruby.
View def.shard.rb
#!/usr/bin/env ruby -w
def defit
def ohai
puts "hello from #{self.class.inspect} #{self.inspect}"
end
puts "called defit in #{self.class.inspect} #{self.inspect}"
end
View definitions.rb
class MyClass
def foo
puts "MyClass -> foo"
bar
end
def bar
puts "MyClass -> bar"
end
end
View city_finder.rb
cities = ('a'..'p').to_a
def generate_lists(cities)
pos = cities.combination(3).to_a.shuffle; nil
map = {}
cities.each do |c|
taken = []
while taken.length < 3
c3 = pos.shift
if c3.include? c
View private_clobbering.rb
Marks-MacBook-Pro:code_scraps markjosef$ irb
irb(main):001:0> require './private_clobbering'
=> true
irb(main):002:0> obj = MyClass.new
=> #<MyClass:0x007ff8220fc680>
irb(main):003:0> obj.public_a
Public A
Private B
=> nil
irb(main):004:0> obj.public_b
@mark
mark / gist:12cb9f08bf943101aa69
Last active Aug 29, 2015
Paper Soccer rules
View gist:12cb9f08bf943101aa69

Paper Soccer

This is a pencil-and-paper version of the game soccer. Two players compete to get the ball into the opposing player's goal, scoring a point. Each point is played on a different field, and there are two fields per page.

The first player with 3 points wins the game.

How to play:

The ball starts at the center of the field (marked by a small circle). For the first point, flip a coin to decide who goes first; for later points, the player who just scored goes second.

@mark
mark / class_method_double.rb
Created Aug 1, 2014
Redefining a stubbed method, using Mocha
View class_method_double.rb
require 'minitest/spec'
require 'mocha/setup'
require 'minitest/autorun'
class MyClass
class << self
attr_accessor :bar
end
You can’t perform that action at this time.