Skip to content

Instantly share code, notes, and snippets.

View alloy's full-sized avatar

Eloy Durán alloy

View GitHub Profile
@alloy
alloy / gist:6339
Created August 20, 2008 08:07 — forked from mmower/gist:6338
def f(&b)
var_inside_method = :foo
binding_of_this_method = binding
binding_of_block = b.binding
binding_inside_block = b.call
eval "p local_variables", binding_of_this_method
eval "p local_variables", binding_of_block
eval "p local_variables", binding_inside_block
module CargoCultGenerator
class << self
# Creates a useful class name based on the description of it's pupose provided.
def class_name(good_descripition_of_purpose, position_of_stars = nil)
who_cares = nil if good_descripition_of_purpose
dude_serious? if position_of_stars
'Base'
end
# Creates useful files like this one: http://github.com/technoweenie/restful-authentication/tree/master/lib/authorization.rb
@alloy
alloy / gist:22408
Created November 5, 2008 19:33 — forked from tenpaiyomi/gist:22407
class MenuBarController
def initialize
# Create our status bar for the application
puts "Initializing....."
# menuZone = NSMenu.menuZone()
# menu = NSMenu.allocWithZone(menuZone)
menuItem = NSStatusBar.systemStatusBar.statusItemWithLength(NSVariableStatusItemLength)
# And set it's details
15:01 j`ey: go go go suggestions!
15:02 alloy: infinite loo
15:02 alloy: p
15:02 alloy: what comes out must go in
15:02 alloy: crease and irons
15:02 j`ey: thats quite an abstract thing :p
15:02 alloy: or: irons and crease
15:02 j`ey: ooh
@alloy
alloy / gist:51272
Created January 23, 2009 23:58
µspec & µmock: A 487 characters spec and mock framework on test/unit
require 'test/unit'
# from 'test' to 'mock' is the `it' code, from 'mock' onwards it's mocking.
module Test::Unit;T='test';class Failure;def long_display;"#{l=location;Array===l ?l[0]:l}\n#{message}".sub(T,'');end;end;$M={};class TestCase;def self.it(n,&b);define_method(T+n,&b)end;'mock';def m(m,n,v);$M[n]=[m,n,m.method(n),caller[0],v];eval"def m.#{n};m=$M['#{n}'];m[5]=true;m[4];end"end;def teardown;$M.map{|_,m|(class<<m[0];self;end).send(:define_method,m[1],m[2].to_proc);m}.map{|m|$M.delete(m[1]);add_failure("#{m[0].inspect} didn't receive :#{m[1]}",m[3])if !m[5]}end;end;end
if $0 == __FILE__
# Test case
class TestObject
def self.a_class_method
false
% script/generate model GrandParent name:string
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/grand_parent.rb
create test/unit/grand_parent_test.rb
create test/fixtures/grand_parents.yml
create db/migrate
create db/migrate/20090127182616_create_grand_parents.rb
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:1:18: error: ruby.h: No such file or directory
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:2:21: error: ruby/io.h: No such file or directory
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c: In function ‘macruby_test_io’:
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:8: error: ‘errno’ undeclared (first use in this function)
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:8: error: (Each undeclared identifier is reported only once
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:8: error: for each function it appears in.)
/Users/eloy/Documents/DEVELOPMENT/MacRuby/macruby-git-svn/test-macruby/fixtures/IOWaitReadable.c:8: error: ‘EAGAIN’ undeclared (first use in this
require "pp"
class Builder
def initialize
@elements = []
end
%w{ html body h1 ul li }.each do |tag|
eval %{
def #{tag}(*args)
class Foo
def self.string_query_accessor(attr, *true_values)
true_values.each do |true_value|
class_eval %{
def #{true_value}?
send(:#{attr}) == '#{true_value}'
end
}, __FILE__, __LINE__
end
end
@alloy
alloy / gist:68988
Created February 23, 2009 15:19
A Hash differ that needs to be cleaned and wrapped as a real gem imo.
class Hash
def diff(other)
result = {}
union = keys | other.keys
result[:not_in_other] = (union - keys).inject({}) { |memo, k| memo[k] = other[k]; memo }
result[:not_in_self] = (union - other.keys).inject({}) { |memo, k| memo[k] = self[k]; memo }
result[:not_equal] = {}
each do |key, value|