Skip to content

Instantly share code, notes, and snippets.

View sfcgeorge's full-sized avatar
🦔

Simon George sfcgeorge

🦔
View GitHub Profile
@sfcgeorge
sfcgeorge / concern.rb
Last active February 7, 2018 16:42
Tweaked ActiveSupport::Concern to work around Opal issue
module ActiveSupport
# A typical module looks like this:
#
# module M
# def self.included(base)
# base.extend ClassMethods
# base.class_eval do
# scope :disabled, -> { where(disabled: true) }
# end
# end
@sfcgeorge
sfcgeorge / telephone_words.rb
Created September 7, 2017 20:44
Telephone Words
require "pp"
MAPPING = {
1 => [],
2 => %w(A B C),
3 => %w(D E F),
4 => %w(G H I),
5 => %w(J K L),
6 => %w(M N O),
7 => %w(P Q R S),
@sfcgeorge
sfcgeorge / json_slop.rb
Created April 11, 2017 10:17
Use JSON as a Ruby object like you just don't care, if you like OpenStruct you'll love JsonSlop!
require "json"
class JsonSlop
def initialize(json)
@hash = json.is_a?(String) ? JSON.parse(json) : json
end
def call
JsonSlopProxy.new(@hash)
end
@sfcgeorge
sfcgeorge / DynamicMethodCall.java
Last active February 12, 2017 20:29
Java class to call methods on objects/classes when given the method name as a String with actual parameters.
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
/**
* We wrote DynamicMethodCall as a helper class to allow writing wrapper
* classes in Java by passing methods as data to call later.
* It allows you to call methods on an object where the method name is passed
* as a String. This is useful for simplifying anonymous method usage like GUI
* event handler, for example. It is capable of calling inherited methods,
@sfcgeorge
sfcgeorge / opts_to_ivars.rb
Created March 17, 2016 10:59
Passing a lot of variables into a mailer that I want to access from the view, so decided to dynamically set the instance variables from opts
class DynamicIvars
def greet(**options)
options.each { |k, v| instance_variable_set :"@#{k}", v }
p @hello
end
end
DynamicIvars.new.greet hello: "world"
# => "world"
@sfcgeorge
sfcgeorge / ackermann.py
Created September 18, 2015 17:32
Various simple AI search algorithms in Python from uni
# Classic Ackermann explosively exponential function
# some attempt at optimization though better optimizations are known
import sys
from sys import setrecursionlimit
sys.setrecursionlimit(50000)
depthM = 5
depthN = 1
answers = []
@sfcgeorge
sfcgeorge / game_of_life.rb
Created August 8, 2015 22:13
Conway's Game of Life in Ruby, aiming to be short but readable
class String
def reverse_color; "\033[7m#{self}\033[27m" end
end
class Cell
attr_accessor :alive
def initialize(neighbors)
@neighbors = neighbors
@alive = @fate = [false, true].sample
@sfcgeorge
sfcgeorge / terminal_commands
Last active August 29, 2015 14:20
Compile Crystal code from external app
crystal run test.cr
#=> "foo"
crystal build test.cr
export CRYSTAL_PATH=/usr/local/Cellar/crystal/0.7.1/src
./test
#=>
ld: library not found for -levent
clang: error: linker command failed with exit code 1 (use -v to see invocation)
@sfcgeorge
sfcgeorge / tail_call_optimization.rb
Last active August 29, 2015 14:18
MRI Ruby optional tail call optimization
# Comment the below compile_options out and
# this will crash with stack level too deep.
# Based on http://timelessrepo.com/tailin-ruby
RubyVM::InstructionSequence.compile_option = {
tailcall_optimization: true,
trace_instruction: false
}
RubyVM::InstructionSequence.new(<<-CODE).eval
@sfcgeorge
sfcgeorge / Gemfile
Created October 8, 2014 00:08
Blink(1) Minitest Reporter
group :test do
gem 'minitest-reporters'
gem 'rb-blink1'
end