Skip to content

Instantly share code, notes, and snippets.

class << Object
def my_aa(sym)
define_method(sym) do
instance_variable_get("@#{sym}")
end
define_method("#{sym}=") do |v|
instance_variable_set("@#{sym}", v)
end
end
class Hash
def method_missing(name, *args)
match = /get_(.*)/.match(name.to_s)
if match and match[1]
puts match[1]
self.class_eval do
define_method(name) do
return self[match[1]]
end
end
# Replace Inheritance with Delegation
# http://refactoring.com/catalog/replaceInheritanceWithDelegation.html
# class Roster < Array
# attr_accessor :title
# def to_s
# out = "#{title}\n"
# each do |item|
# out += "#{item}\n"
File.new('/tmp/http-log', 'w') do |f|
http = Net::HTTP.new
http.set_debug_output(f)
http.start { .... }
end
$ java -cp clojure.jar clojure.main ~/Projects/Clojure/fake-cs.clj
Sending...
#<Agent@1f758500: {:name Music, :online false}>
Starting...
Exception in thread "main" java.lang.NullPointerException (fake-cs.clj:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.load(Compiler.java:5857)
at clojure.lang.Compiler.loadFile(Compiler.java:5820)
at clojure.main$load_script.invoke(main.clj:221)
@shepmaster
shepmaster / gist:1236700
Created September 23, 2011 03:37
Strange behavior with respond_to matcher
describe "should respond_to" do
it "works the same as respond_to?" do
group = RSpec::Core::ExampleGroup.describe {}
a = group.new
a.respond_to?(:be).should be_true
a.should respond_to(:be)
end
end
;; Note that the equal test got closed too early...
(deftest bad-test
(is (= {:a 1})
{:a 1}))
;; Has the error:
;; error in process filter: Wrong number of arguments: nil, 6
@shepmaster
shepmaster / gist:1447264
Created December 8, 2011 15:18
Learned a new thing that should have been obvious in retrospect...
class Foo
attr_accessor :a
def initialize
@a = 1
end
end
foo = Foo.new
puts foo.a
@shepmaster
shepmaster / privacy_filter.rb
Created December 12, 2011 04:15 — forked from steveklabnik/privacy_filter.rb
A spec for a filter
class PrivacyFilter
class << self
def filter(controller)
@controller = controller
first_article? or administrator? or user?
end
def first_article?
@controller.params[:id] == 1
@shepmaster
shepmaster / gist:1886730
Created February 22, 2012 19:17
Overriding #new
class A
def self.new(arg1, arg2)
B.new(arg2, arg1)
end
end
class B
def initialize(alpha, beta)
puts "#{alpha}, #{beta}"
end