Skip to content

Instantly share code, notes, and snippets.

View certainty's full-sized avatar
🏠
Working from home

David Krentzlin certainty

🏠
Working from home
View GitHub Profile
module Baz
def bar
"Baz#bar"
end
end
class Foo
def bar
"Foo#bar"
end
@certainty
certainty / gist:3245488
Created August 3, 2012 07:43
named let ruby
def nlet(*a)
(loop = lambda{ |*a| yield(loop,*a) }).call(*a)
end
nlet(10,20){ |loop,x,y|
puts "Next round"
if x < 20
loop[x+1,y+1]
end
}
@certainty
certainty / gist:3792549
Created September 27, 2012 06:45
Method chaining in try
class Symbol
def >>(other)
[self,other]
end
end
class Array
def >>(other)
self + [other]
end
@certainty
certainty / gist:3792694
Created September 27, 2012 07:33
generalized method chaining and array as applicative
class Array
def >>(other); self + [other]; end
def to_proc(*args)
->(r) { inject(r){ |o,(m,*args)| o.send(m,*args) } }
end
def apply(rec)
self.to_proc[rec]
end
module HomeHelper
def display_category(category)
posts_count = content_tag(:span, category.posts.count)
category_maybe_link = category.allow_browsing? ? link_to(category.name, link_to_arguments) : category.name
header = content_tag(:h3, category_maybe_link.html_safe + posts_count)
content_tag(:li) do
header + content_tag(:ul, :class => "main") do
@certainty
certainty / gist:3851071
Created October 8, 2012 06:42
embedded_roles
module EmbeddedRoles
def self.included(base)
base.extend(ClassMethods)
base.send(:include,InstanceMethods)
end
module ClassMethods
def embedded_roles(roles)
singleton_class.send(:define_method,:available_roles){ roles }
@certainty
certainty / usage
Created October 12, 2012 09:23
serial vs. parallel
(use http-client html-parser sxpath)
(define +sites+ (list "http://google.com" "http://google.de" "http://spiegel.de" "http://focus.de" "http://golem.de" "http://planetrubyonrails.org" "http://rubyonrails.org"))
(define (fetch-site site)
(let ((site (call-with-input-request site #f html->sxml)))
((car-sxpath '(// title)) site)))
(define (spawn-fetcher site)
(let ((t (make-thread (lambda () (fetch-site site)))))
@certainty
certainty / gist:3885785
Created October 13, 2012 19:06
dynamic attr
class Module
def dynamic_attr(*names)
attr_accessor *names
names.each do |name|
define_method("with_#{name}") do |new_value,&blk|
begin
old_value = __send__(name)
__send__("#{name}=",new_value)
blk.call(self)
ensure
/**
* @class Ext.omcx.I18N.PackedObject
*
*/
Ext.extend(Ext.omcx.I18N.PackedObject,Ext.util.Observable,{
/**
* Lookup the object stored under the given key
* @methodOf Ext.omcx.I18N.PackedObject
* @param {String} key
* @params default_value Will be returned if no entry can be found for key
shared_examples "foo" do |a,args|
it "is true" do
true.should be_true
end
end
describe "some context" do
def self.user
:user