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
# Goal: Allow addition of instances to a collection in a factory-built object | |
# when those instances require references to the parent. | |
# Typically occurs in Rails when one model has_many instances of another | |
# See more at: | |
# http://stackoverflow.com/questions/2937326/populating-an-association-with-children-in-factory-girl | |
class Factory | |
def has_many(collection) | |
# after_build is where you add instances to the factory-built collection. |
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
class Foo | |
CONST_ON_FOO = "the constant on Foo" | |
class << self | |
puts "When we're defining CONST_ON_FOOS_CLASS, we're actually in: #{self}, a #{self.class}" | |
CONST_ON_FOOS_CLASS = "the constant on Foo's class" | |
end | |
end | |
instance = Foo.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
begin | |
begin | |
raise Exception.new("An Exception") | |
rescue => e | |
puts "Rescued an unspecified error: #{e.message}" | |
end | |
rescue Exception => e | |
puts "Rescued an exception: #{e.message}" | |
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
$ npm test | |
> favicons-webpack-plugin@0.0.7 test D:\projects\favicons-webpack-plugin | |
> ava | |
loudRejection/api is deprecated. Use the currently-unhandled module instead. | |
4 passed | |
3 failed |
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
module InstanceCache | |
def self.included(receiver) | |
receiver.extend(ClassMethods) | |
end | |
module ClassMethods | |
# Override me for good times | |
def find_by_cache_key(cache_key) | |
find(cache_key) |
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
module A | |
def a_method | |
"a method" | |
end | |
end | |
module B | |
extend A | |
def b_method |
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 | |
search = Regexp.new(ARGV[0]) | |
output = `git branch` | |
matching = output.lines.select{ |l| l.match(search) } | |
if matching.size == 0 | |
STDERR.puts "No branch matches #{ARGV[0]}" | |
elsif matching.size == 1 | |
`git checkout #{matching[0].strip}` | |
else | |
puts "Multiple branches matched:" |
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 "rubygems" | |
require 'activesupport' | |
file_name = ARGV[0].underscore.gsub(/_test$/, "") | |
test_name = ARGV[1].gsub(" ", "_") if ARGV[1] | |
paths = [ | |
"test/unit/#{file_name}_test.rb", | |
"test/unit/helpers/#{file_name}_test.rb", |
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
class Hash | |
# Pass either a default value or a block to return it (a la Hash#fetch()). | |
# If both are passed, the block will take precedence over the default value | |
def evoke(key, default = nil) | |
if include?(key) | |
self[key] | |
else | |
self[key] = block_given? ? yield : default | |
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
# Out of the box, Ruby's Rational class doesn't know how to multiply or divide a BigDecimal; | |
# You get: "TypeError: Rational can't be coerced into BigDecimal" when you try. | |
class Rational | |
alias :"old_/" :"/" | |
def /(a) | |
begin | |
# Keep existing behavior if we can | |
send(:"old_/", a) | |
rescue TypeError |
NewerOlder