Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
michaelfeathers / c11r.rb
Created September 22, 2020 22:43
Proof of Concept for interactive creation of characterization tests
# Written by Michael Feathers July 10th, 2020
class Command
def run line, session
return unless matches? line
process line, session
end
end
class FixView < Command
using UnityEngine;
public class JumpingCharacterControllerExample : MonoBehaviour
{
[SerializeField] private float jumpHeight;
[SerializeField] private float timeToReachJumpApex;
private Vector3 _velocity;
private Vector3 _oldVelocity;
@aninder
aninder / redflag.rb
Created March 5, 2013 12:05
fix for bug in "metaprogramming ruby" book code snippet on page 121 (creating DSL). It calls events of previous files again for each new file loaded and redflag2.rb is another way creating it.
lambda {
setups = []
events = {}
Kernel.send :define_method, :event do |name, &block|
events[name] = block
end
Kernel.send :define_method, :setup do |&block|
setups << block
@andkerosine
andkerosine / raskell.rb
Created August 15, 2012 05:56
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
@nusco
nusco / symbol_to_proc.rb
Created August 18, 2010 15:36
Spell: Symbol to Proc
# =====================
# Spell: Symbol to Proc
# =====================
# Convert a symbol to a block that calls a single method.
[1, 2, 3, 4].map(&:even?) # => [false, true, false, true]
# For more information: http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby
@nusco
nusco / shared_scope.rb
Last active September 5, 2015 18:55
Spell: Shared Scope
# ===================
# Spell: Shared Scope
# ===================
# Share variables among multiple contexts in the same Flat Scope (https://gist.github.com/nusco/535082).
lambda {
shared = 10
self.class.class_eval do
@nusco
nusco / self_yield.rb
Created August 18, 2010 15:32
Spell: Self Yield
# =================
# Spell: Self Yield
# =================
# Pass self to the current block.
class Person
attr_accessor :name, :surname
def initialize
@nusco
nusco / scope_gate.rb
Created August 18, 2010 15:31
Spell: Scope Gate
# =================
# Spell: Scope Gate
# =================
# Isolate a scope with the class, module, or def keyword.
a = 1
defined? a # => "local-variable"
module MyModule
@nusco
nusco / sandbox.rb
Created August 18, 2010 15:30
Spell: Sandbox
# ==============
# Spell: Sandbox
# ==============
# Execute untrusted code in a safe environment.
def sandbox(&code)
proc {
$SAFE = 2
yield