Skip to content

Instantly share code, notes, and snippets.

# for Ruby 1.9.2 compatibility
gem 'ruby-debug-base19', "0.11.24"
gem 'ruby-debug19', "0.11.6"
@rbxbx
rbxbx / antialias_method.rb
Created October 23, 2010 04:28
Object.antialias_method
class Object
def self.antialias_method(new_name, old_name)
define_method(new_name) { !send(old_name) }
end
end
require 'test/unit'
class AntialiasTest < Test::Unit::TestCase
class Person
@rbxbx
rbxbx / lambda_methods.rb
Created December 14, 2010 01:36
define methods on a lambda, who knew. Well, probably a lot of you.
Objector = lambda do |properties|
lambda do |meth|
properties[meth]
end
end
def Objector.new(props); self[props] end
#closures as I currently understand them
free_variable = 1
clos = lambda { p "I'm a closure because I hold on to #{free_variable += 1}" }
non_clos = lambda { |x| p "I'm not a closure :(, because #{x += 1} hasn't changed" }
p clos.call
p clos.call
@rbxbx
rbxbx / hash_map_keys.rb
Created January 12, 2011 21:10
Hash#map_keys
class Hash
def map_keys(&blk)
Hash[keys.map(&blk).zip(values)]
end
end
@rbxbx
rbxbx / pinboarder.rb
Created January 30, 2011 21:40
Import and mirror your http://pinboard.in bookmarks because you're SCARED.
require 'sinatra'
require 'nokogiri'
require 'open-uri'
require 'haml'
require 'sequel'
require 'sqlite3'
get '/' do
redirect '/update' unless bookmarks_table_exists?
haml :index, :locals => { bookmarks: bookmarks_table }
@rbxbx
rbxbx / try_chain.rb
Created February 8, 2011 21:55
eeevil
class Object
def try_chain *meths
meths.inject(self) do |res, meth|
break unless result = res.try(meth)
result
end
end
end
@rbxbx
rbxbx / maybe.rb
Created February 9, 2011 17:38
Object#maybe - A (crude?) implementation of the Maybe monad in Ruby
class BlankSlate
instance_methods.each { |m| undef_method m unless m =~ /^__/ }
end
class MaybeProxy < BlankSlate
attr_reader :obj
def initialize(obj)
@obj = obj
end
@rbxbx
rbxbx / incrudable.rb
Created March 3, 2011 14:35
CRUD with decent_exposure
# pulled out of a project authored by tpope && rbxbx
# not generic enough for general use, but a decent example of
# an application specific restful crud abstraction
module Incrudable
extend ActiveSupport::Concern
included do
expose(controller_name) { controller_name.classify.constantize.scoped }
expose(controller_name.singularize)
Feature: Get
In order to interact with the command line
As an end user
I want to see and enter things
Scenario:
Given I will see "Private?"
Then I will enter "Yes, sir?"
Then I will see "Jump"