Skip to content

Instantly share code, notes, and snippets.

@xaviershay
xaviershay / gist:6200981
Created August 10, 2013 16:02
Diff of `rspec-mocks` to remove circular dependency. Could be a problem if another rspec library calls into mocks then the line returned would be in the that library. This would be consistent with the behaviour that was here before `CleanCaller` was introduced though, and presumably if other libraries want to remove themselves from a trace that'…
diff --git a/lib/rspec/mocks/clean_caller.rb b/lib/rspec/mocks/clean_caller.rb
index dc52c87..ed2a72d 100644
--- a/lib/rspec/mocks/clean_caller.rb
+++ b/lib/rspec/mocks/clean_caller.rb
@@ -6,21 +6,10 @@ module RSpec
# the code using the library, which is far more useful than the particular
# internal method that raised an error.
class CleanCaller
-
- # This list is an unfortunate dependency on other RSpec core libraries.
@xaviershay
xaviershay / befunge_compiler_prototype.rb
Created July 17, 2013 03:59
A very quick sketch of a befunge compiler.
class ValueObject < Struct
def self.[](*args)
if args.length > 0
new(*args)
else
new(:null)
end
end
def [](*args)
@xaviershay
xaviershay / repro.rb
Created July 5, 2013 21:31
Unexpected behaviour defining Ruby classes anonymously.
true
repro.rb:16:in `block in <main>': uninitialized constant B (NameError)
from repro.rb:13:in `initialize'
from repro.rb:13:in `new'
from repro.rb:13:in `<main>'
@xaviershay
xaviershay / gist:5895834
Last active December 19, 2015 04:18
Diff with http://xaviershay.github.io/writing/docs/1_sos.html to collapse `apply?` and `reducible?` into an identity function. I'm currently thinking I will introduce this after I've had to introduce equality definitions for a different reason. For the purposes of explaining the mathematical notation, I feel like it is unnecessary conceptual ove…
diff --git a/code/1_sos.rb b/code/1_sos.rb
index 03bd37e..8de6d9b 100644
--- a/code/1_sos.rb
+++ b/code/1_sos.rb
@@ -34,7 +34,8 @@ end
Number = Struct.new(:value) do
def to_s; value.to_s end
def inspect; "«#{self}»" end
- def reducible?; false end
+ def ==(other); other.is_a?(Number) && value == other.value end
months = {
'Jan' => 6,
'Feb' => 2,
'Mar' => 2,
'Apr' => 5,
'May' => 0,
'Jun' => 3,
'Jul' => 5,
'Aug' => 1,
'Sep' => 4,
@xaviershay
xaviershay / rewrite.rb
Created January 10, 2013 06:11
Proof-of-concept squid proxy settings for proxying http://rubygems.org/ Tested on one project on my local machine both cold, and warm with internet turned off.
#!/usr/bin/env ruby
# url rewriter for rubygems squid proxy
STDOUT.sync = true
while line = gets
url = line.split(' ')[0]
# Cargo-culted this conditional, not sure if it is necessary
response = if url
diff --git a/lib/ruby/shared/jruby/core_ext/class.rb b/lib/ruby/shared/jruby/core_ext/class.rb
index eca4b54..39c88d6 100644
--- a/lib/ruby/shared/jruby/core_ext/class.rb
+++ b/lib/ruby/shared/jruby/core_ext/class.rb
@@ -140,7 +140,7 @@ class Class
annotations.each_with_index do |param_annos, i|
for cls, params in param_annos
params ||= {}
- self_r.add_parameter_annotation(name, i, _anno_class(cls))
+ self_r.add_parameter_annotation(name, i, _anno_class(cls), params)
class RegistrationsController < ApplicationController
include Injector::ControllerMethods
provided_by Controller::Registration
end
@xaviershay
xaviershay / controller_source.rb
Created October 9, 2012 04:56
What if we used dependency injection for Rails controller code?
class ControllerSource
Response = Struct.new(:controller, :injector) do
def redirect_to(path, *args)
controller.redirect_to(controller.send("#{path}_path", *args))
end
def render(*args)
ivars = {}
if args.last.is_a?(Hash) && args.last.has_key?(:ivars)
ivars = args.last.delete(:ivars)
@xaviershay
xaviershay / service_service.rb
Created September 16, 2012 04:36 — forked from tobias/service_service.rb
A TorqueBox service that consumes from a queue.
class ServiceService
def initialize(options)
@queue = TorqueBox::Messaging::Queue.new( '/queues/service_request' )
@running = true
end
def start()
Thread.new do
while @running