Skip to content

Instantly share code, notes, and snippets.

View lazyatom's full-sized avatar
🤠
Yes

James Adam lazyatom

🤠
Yes
View GitHub Profile

How Should We Use Struct?

The Choice

It's common in Ruby to see some code setup a Struct like this:

class Specialized < Struct.new(:whatever)
  # ... define custom methods here...
end
@lazyatom
lazyatom / 0-readme.md
Created June 13, 2012 19:15 — forked from sj26/0-readme.md
ruby-1.9.3-p194 cumulative performance patch.

Patched ruby 1.9.3-p194 for 30% faster rails boot

What is?

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
@lazyatom
lazyatom / hooks.rb
Created August 17, 2009 16:25 — forked from defunkt/hooks.rb
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
@lazyatom
lazyatom / hooks.rb
Created August 17, 2009 08:45 — forked from defunkt/hooks.rb
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