Skip to content

Instantly share code, notes, and snippets.

View JacobNinja's full-sized avatar

JR Richardson JacobNinja

View GitHub Profile
require 'rubygems'
require 'active_support'
class Logbook
def initialize
@entries = {}
end
@JacobNinja
JacobNinja / gist:1425526
Created December 3, 2011 00:36
Test stubs
# Creates test stubs for logic in methods
require 'rspec'
require File.expand_path('../../test_stubs.rb', __FILE__)
describe TestStubs do
let(:sut) { TestStubs.new(@code) }
describe "#generate" do
@JacobNinja
JacobNinja / Gemfile
Created June 22, 2012 02:21 — forked from mattjmorrison/Gemfile
Dynamic Thor Group
source 'https://rubygems.org'
gem 'thor'
@JacobNinja
JacobNinja / gist:3537082
Created August 30, 2012 18:39
Rubinius parser confusion
# Here's the code being evaluated
# rubinius-2.0.0dev
html_class = build_class("") {|c| c << "required" if required }
# => "required"
### FIRST EXAMPLE
# Symbolic expression tree with no arguments to build_class
'html_class = build_class() {|c| c << "required" if required }'.to_sexp
@JacobNinja
JacobNinja / gist:3868668
Created October 10, 2012 21:47
Parsing the parser
p SexpBuilder.parse File.read(__FILE__) if $0 == __FILE__
# => [s(:call, nil, :require, s(:str, "sexp_processor")), s(:call, nil, :require, s(:str, "ripper")), s(:call, nil, :require, s(:call, s(:const, :File), :expand_path, s(:str, "./../nodes/all"), s(:str, "(string)"))), s(:class, :SexpBuilder, s(:colon2, s(:const, :Ripper), :SexpBuilderPP), s(:defs, s(:self), :parse, s(:args, :code, :filename, s(:block, s(:lasgn, :filename, s(:str, "(string)")))), s(:lasgn, :ast, [:super, [s(:lvar, :code), s(:lvar, :filename)]]), s(:call, s(:lvar, :ast), :[], s(:lit, 1))), s(:cdecl, :KEYWORDS, s(:array, s(:str, "true"), s(:str, "false"), s(:str, "self"), s(:str, "nil"))), s(:cdecl, :BINARY, s(:hash, s(:lit, :"&&"), s(:colon2, s(:const, :Nodes), :BooleanAnd), s(:lit, :and), s(:colon2, s(:const, :Nodes), :LogicalAnd), s(:lit, :"||"), s(:colon2, s(:const, :Nodes), :BooleanOr), s(:lit, :or), s(:colon2, s(:const, :Nodes), :LogicalOr), s(:lit, :"||="), s(:colon2, s(:const, :Nodes), :OptionalAssignment))), s(:cdecl, :MAGIC_VARI
@JacobNinja
JacobNinja / heightmap.rb
Created December 12, 2012 05:50
tile pathfinder
def route_to(start_x, start_y, end_x, end_y, path=[], last_routes=[])
possible_routes = sorted_routes(start_x, start_y, end_x, end_y)
if r = possible_routes.find {|route| route == [end_x, end_y]}
return path + [[*r, new_item_height(*r)]]
else
possible_routes.each do |(possible_x, possible_y)|
next if last_routes.include? [possible_x, possible_y]
new_path = path + [[possible_x, possible_y, new_item_height(possible_x, possible_y)]]
used_routes = (last_routes + possible_routes).uniq
new_route = route_to(possible_x, possible_y, end_x, end_y, new_path, used_routes)
class SetupTestCase < Test::Unit::TestCase
def self.test(test_name, *setups, &block)
test_with_setups = lambda do
setup_procs = setups.map {|setup_func_name| respond_to?(setup_func_name) ? method(setup_func_name) : nil }.compact
setup_procs.each(&:call)
block.bind(self).call
end
super(test_name, &test_with_setups)
end
@JacobNinja
JacobNinja / gist:5017303
Created February 22, 2013 23:08
Lazy sequences in Ruby
lazy_expensive_sequence = Enumerator.new do |y|
offset = 0
amount_to_retrieve = 50
loop do
## This may not be memory safe if we try to retrieve all at once, so we safely retrieving an amount that we know will fit in memory
retrieve_memory_hogs(offset, amount_to_retrieve).each do |i|
y << i
end
offset += amount_to_retrieve
end
@JacobNinja
JacobNinja / gist:5104046
Last active December 14, 2015 14:58
Initial implementation of Reducers in Ruby
def fib(n)
if (n < 2)
n
else
fib(n-1) + fib(n-2)
end
end
JRuby 1.7.2
@JacobNinja
JacobNinja / ruby
Last active December 15, 2015 13:29 — forked from Jimgerneer/ruby
class CommitScore
ScoreValue = Struct.new(:email, :score)
def initialize(commit_values)
@commit_values = commit_values
end
def score
grouped_commits_by_email.map {|(email, commits) score_value(email, commits) }