Skip to content

Instantly share code, notes, and snippets.

@txus
txus / delegate_matcher.rb
Created February 2, 2011 09:19
RSpec matcher for delegations
# RSpec matcher to spec delegations.
#
# Usage:
#
# describe Post do
# it { should delegate(:name).to(:author).with_prefix } # post.author_name
# it { should delegate(:month).to(:created_at) }
# it { should delegate(:year).to(:created_at) }
# end
@txus
txus / serialize_matcher.rb
Created February 24, 2011 10:05
RSpec matcher for serialized ActiveRecord columns
# RSpec matcher to spec serialized ActiveRecord attributes.
#
# Usage:
#
# describe Post do
# it { should serialize(:data) } # serialize :data
# it { should serialize(:registers).as(Array) } # serialize :registers, Array
# it { should serialize(:options).as(Hash) } # serialize :options, Hash
# end
@txus
txus / bulk_doubles.rb
Created February 26, 2011 18:44
Implementation draft of bulk doubles, an RSpec::Mocks feature request
module RSpec
module Mocks
module BulkDoubles
module ClassMethods
# Declares a bunch of test doubles at once.
#
# +name+ will be both the name of the double and the method name used
# to invoke it.
#
@txus
txus / transformations.rb
Created February 27, 2011 15:55
Draft implementation of ActiveModel::Transformations
# See Mike Perham's proposal on ticket: https://rails.lighthouseapp.com/projects/8994/tickets/6477-activemodel-transformations
module ActiveModel
# == Active Model Transformations
#
# Provides a set of callbacks to perform transformations on your model
# attributes before being assigned.
#
# A minimal implementation could be:
@txus
txus / Command line
Created March 30, 2011 13:22
Test Framework Benchmark
$ gem install minitest rspec
$ time ruby rspec.rb && time ruby minispec.rb && time ruby minitest.rb
................
Finished in 0.01554 seconds
16 examples, 0 failures
ruby rspec.rb 0.21s user 0.08s system 99% cpu 0.290 total
-------------------------------
@txus
txus / config
Created April 4, 2011 07:42
Paymo to cashboard converter. WIP
---
BUNDLE_DISABLE_SHARED_GEMS: "1"
@txus
txus / class_instance_variables.rb
Created April 5, 2011 09:02
Class variables are shared by all subclasses, while *class instance variables* are not.
class Person
# @count is a CLASS INSTANCE VARIABLE exclusive to Person.
# Only when you instantiate a Person (not a subclass of Person),
# the count increases.
@count = 0
def initialize
self.class.count += 1
end
@txus
txus / unbalanced_stack.rb
Created May 15, 2011 16:41
Unbalanced stack error when dealing with labels (rbx VM)
# local 0 is an array
# local 1 is a pointer which serves as an index of that array
# Check the contents of the new cell
g.push_local 0
g.push_local 1
g.send :[], 1, false
fin = g.new_label
g.git fin
@txus
txus / invalid_local_variable_access.rb
Created May 15, 2011 19:23
Invalid local variable access
# This stage takes a tree of Brainfuck::AST nodes and
# simply calls the bytecode method on them.
class Generator < Rubinius::Compiler::Stage
next_stage Rubinius::Compiler::Encoder
attr_accessor :variable_scope, :root
def initialize(compiler, last)
super
@compiler = compiler
@variable_scope = nil
module SomeLanguage
module AST
class SomeNode
def bytecode(g)
g.push_local 1
g.meta_push_1
g.meta_send_op_plus 0
g.set_local 1
# Check the contents of the new cell