Skip to content

Instantly share code, notes, and snippets.

View lmars's full-sized avatar

Lewis Marshall lmars

View GitHub Profile
@lmars
lmars / README.md
Created December 11, 2011 23:01
Game Of Life Rules

Game Of Life Rules

You have a grid of cells in 2 dimensions. Each cell has 2 possible states, alive or dead. Each cell has 8 neighbours: above, below, left, right, and the 4 diagonals.

  • any life cell < 2 neighbours dies
  • any life cell > 3 neighbours dies
  • any live cell with 2 or 3 neighbours lives to next generation
  • any dead cell with exactly 3 live neighbours becomes a live cell
@lmars
lmars / gist:1939629
Created February 29, 2012 10:16
Numeral to digit
NUMERALS = {
'i' => 1,
'v' => 5,
'x' => 10,
'l' => 50,
'c' => 100,
'd' => 500,
'm' => 1000
}
@lmars
lmars / gist:1939737
Created February 29, 2012 10:27
Iterating over a string
string = 'ABCDE'
string.reverse.each_char do |numeral|
# inside this block numeral will be a character from the string
puts numeral
end
# This will output:
E
D
@lmars
lmars / redirect_to_matcher.rb
Created September 5, 2012 21:00
Sinatra based domain redirect app
class RedirectTo
def initialize(expected, session)
@expected = expected
@session = session
end
def matches?(visit_proc)
instance_eval(&visit_proc)
@status = @session.status_code
@lmars
lmars / cap.sh
Created September 21, 2012 10:33
Capistrano growlnotify
cap() {
bundle exec cap $@
if [ $? -eq 0 ]; then
growlnotify --image=$HOME/Pictures/success.png -m 'Success!' "cap $@"
else
growlnotify --image=$HOME/Pictures/failure.png -m 'Failure!' "cap $@"
fi
}
@lmars
lmars / spec_helper.rb
Created January 14, 2013 10:52
Capybara feature caching
before(:each, :capybara_feature => true)
@cache_store = ActionController::Base.cache_store
ActionController::Base.perform_caching = true
ActionController::Base.cache_store = :memory_store
end
after(:each, :capybara_feature => true)
ActionController::Base.perform_caching = false
ActionController::Base.cache_store = @cache_store
end
#
# start of solution
#
MyString = Class.new(String)
MyString.define_singleton_method(:alphabet) { 'abc' }
MyString.class_eval {
exclaim_method = proc { |*args|
count = args.first || 1
@lmars
lmars / encrypted_zip.rb
Created January 23, 2013 10:22
Create a CSV in memory then add it to an encrypted zip file
require 'zipruby'
require 'csv'
csv = CSV.generate do |csv|
csv << ['i', '2i']
1.upto(10) do |i|
csv << [i, i*2]
end
end
@lmars
lmars / memoize.rb
Created April 17, 2013 10:34
Memoize example
def foo(*args)
@memoized_foo ||= {}
@memoized_foo[args] ||= begin
some_expensive_method
end
end
@lmars
lmars / 00_sync.rb
Last active December 18, 2015 10:29
require 'net/http'
require 'uri'
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new')
ints = []
resp = Ne