This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert_before(klass, options={}) | |
instance = new(nil, options) | |
call_without = "call_without_#{instance.object_id}" | |
call_with = "call_with_#{instance.object_id}" | |
klass.class_eval do | |
define_method call_with do |env| | |
instance.app = proc { |env| send call_without, env } | |
instance.call(env) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self.method_missing(name, *args) | |
name | |
end | |
def function(*param_names, &block) | |
klass = Class.new { attr_accessor :this, *param_names } | |
this = TOPLEVEL_BINDING.eval('self') | |
proc do |*params| | |
context = klass.new |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str = "foobar"[2..-1] | |
p str # => "obar" | |
p str.rindex(/a/) # => 0 | |
p "obar".rindex(/a/) # => 2 | |
str = "foobar"[3..-1] | |
p str # => "bar" | |
p str.rindex(/a/) # => nil | |
p "bar".rindex(/a/) # => 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec/autorun' | |
module Stuff | |
def authenticated_as | |
puts "1: #{self.inspect}" | |
context 'when there is stuff' do | |
puts "2: #{self.inspect}" | |
yield | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module defines an alternative to STI where individual types are | |
# represented by lightweight proxies instead of full models. Each type can have | |
# its own validations, callbacks, and business logic. In general, client code | |
# works with the underlying model class directly instead of the type classes, | |
# but the #as_type method returns a wrapped object that can be used to access | |
# type-specific logic. | |
module HasType | |
INVALID_TYPE_MESSAGE = 'Please choose a valid type' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => ':memory:' | |
) | |
ActiveRecord::Schema.define do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'pry-exception_explorer' | |
require 'pry-exception_explorer/shim_builder' | |
binary_name = "lib_overrides.#{PryExceptionExplorer::ShimBuilder::Dyname}" | |
if !File.exists? File.join PryExceptionExplorer::ShimBuilder.dir, binary_name | |
puts "First run, building shim" | |
PryExceptionExplorer::ShimBuilder.compile | |
puts "Hopefully built...!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[1] pry(main)> def x | |
[1] pry(main)* puts 'called x' | |
[1] pry(main)* callcc { |cc| return cc } | |
[1] pry(main)* end | |
=> nil | |
[2] pry(main)> def y | |
[2] pry(main)* puts 'called y' | |
[2] pry(main)* x.call | |
[2] pry(main)* ensure | |
[2] pry(main)* puts 'ensure block' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Recursively retrieve nested attributes (or method calls) from an object. | |
def get_path(obj, *path) | |
if obj.is_a?(Array) | |
obj.map { |o| get_path(o, *path) } | |
else | |
if path.count > 1 | |
next_obj = obj.send(path.first) | |
get_path(next_obj, *path.drop(1)) | |
else | |
obj.send(path.first) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def draw_chart(collection, attribute=nil, &block) | |
if attribute | |
grouped = collection.group_by(&attribute) | |
elsif block | |
grouped = collection.group_by(&block) | |
else | |
raise "Must provide either an attribute or block to group by!" | |
end | |
# try to sort them, but if they're not sortable, don't worry about it |