It's common in Ruby to see some code setup a Struct
like this:
class Specialized < Struct.new(:whatever)
# ... define custom methods here...
end
This script installs a patched version of ruby 1.9.3-p194 with patches for boot-time performance improvements (#66 and #68), and runtime performance improvements (#83 and #84). It also includes the new backported GC from ruby-trunk.
Huge thanks to funny-falcon for the performance patches.
require 'test/unit/assertions' | |
include Test::Unit::Assertions | |
assert_equal "1.9.2", RUBY_VERSION | |
# A Ruby meta-programming puzzle for polite programmers. | |
# This puzzle was created by Matt Wynne (@mattwynne) on 2011-04-10 inspired by Jim Weirich's | |
# talk at the Scottish Ruby Conference 2011. | |
# | |
# The challenge is: you have a class Foo which you want to monkey-patch, but politely. |
class BetterNews | |
def initialize(string, required) | |
@age = required | |
@string = string | |
end | |
attr_reader :age | |
def method_missing(name, *args) | |
self.class.new(@string.send(name, *args), @age) |
# how do you test algorithms? | |
class Pythagoream < Struct.new(:a, :b) | |
class BadArgument < ArgumentError; end | |
def result | |
raise BadArgument if a < 0 || b < 0 | |
a**2 + b**2 | |
end | |
end |
module Rip | |
module Commands | |
# Runs ~/.rip/active/hooks/used after `rip use` | |
# and ~/.rip/active/hooks/will-leave when moving to another env | |
alias_method :rip_use, :use | |
def use(*args) | |
run_hook_if_exists('will-leave', Rip::Env.active) | |
rip_use(*args) | |
run_hook_if_exists('used', Rip::Env.active) | |
end |
module Rip | |
module Commands | |
# Runs ~/.rip/active/hooks/after-use after `rip use` | |
# and ~/.rip/active/hooks/on-leave when moving to another env | |
alias_method :rip_use, :use | |
def use(*args) | |
run_hook_if_exists('before-leave') | |
rip_use(*args) | |
run_hook_if_exists('after-use') | |
end |
➜ rip (file_conflicts)⚡ $ rip use empty; rip env delete test; rip env create test; rip install monk | |
ripenv: using empty | |
ripenv: deleted test | |
ripenv: created test | |
Successfully installed thor (0.11.5) | |
Some files from wycats-thor (0.11.5) conflict with those already installed by thor: | |
lib/thor | |
lib/thor/actions | |
lib/thor/actions/create_file.rb | |
lib/thor/actions/directory.rb |
context "A user posting to a blog" do | |
setup do | |
@user = Factory(:user) | |
@blog = Factory(:blog) | |
end | |
action do | |
post :create, :user_id => @user.id, :id => @blog.id, :post => {:body => "Blah"} | |
end | |
# Boom | |
Factory.define(:product) do |product| | |
product.handles do |p| # or whatever the has_many is called | |
Array.new(5) { x.association(:handle) } # return an array, as many as you like. | |
end | |
# etc | |
end |