Skip to content

Instantly share code, notes, and snippets.

@RobertFischer
RobertFischer / gist:2773494
Created May 23, 2012 06:04
An assert to affirm a method exists
// The signature that works out more naturally
void assertHasMethod(Object obj, String name, Class[] types) {
def methods = obj.metaClass.respondsTo(obj, name, types)
if(methods.isEmpty()) fail("Could not find method $name($types) on $obj")
}
// Enables [Foo,Bar,Baz] notation
void assertHasMethod(Object obj, String name, List<Class> types) {
assertHasMethod(obj, name, types.toArray(new Class[0]))
}
@RobertFischer
RobertFischer / gist:3055738
Created July 5, 2012 19:06
HashWithIndifferentAccess stores values as strings, becomes string on reverse merge
>> h = HashWithIndifferentAccess.new
=> {}
>> h[:foo] = 'bar'
=> "bar"
>> h[:bar] = 2
=> 2
>> h[:frodo] = 'blah'
=> "blah"
>> h.class
=> HashWithIndifferentAccess
@RobertFischer
RobertFischer / freeze-fail.rb
Created August 27, 2012 18:45
Ruby Ain't Functional, So Freezing Doesn't Propagate
x = ["abc"].freeze
x[0][0] = "xyz"
puts x[0] # "xyzbc"
@RobertFischer
RobertFischer / ampersand.rb
Created September 7, 2012 19:47
Ampersand Equivalency
foos.find(&:bar)
foos.find do |it|
it.bar
end
@RobertFischer
RobertFischer / wtf.rb
Created September 14, 2012 19:55
Ruby erring out because a constant is *NOT* missing
Loaded suite ./test/unit/musicians_test
Started
E.
Finished in 0.027968 seconds.
1) Error:
test_load_one(MusiciansTest):
ArgumentError: Object is not missing constant Musician!
/home/rcfischer/.rvm/gems/ree-1.8.7-2012.02@reverbnation/gems/activesupport-2.2.3/lib/active_support/dependencies.rb:419:in `load_missing_constant'
/home/rcfischer/.rvm/gems/ree-1.8.7-2012.02@reverbnation/gems/activesupport-2.2.3/lib/active_support/dependencies.rb:77:in `const_missing'
@RobertFischer
RobertFischer / WhatWorks.java
Last active December 18, 2015 19:38
ClassCircularityError Avoidance 101
// To avoid the ClassCircularityError, I have to log a message to the root logger before attaching a handler to it.
// But even that has to be done in a very particular way.
// I tried this:
Logger rootLogger = Logger.getLogger("");
rootLogger.log(rootLogger.getLevel(), "Some message");
// But that gives a ClassCircularityError
// Then I tried this:
Logger rootLogger = Logger.getLogger("");
@RobertFischer
RobertFischer / dynamic_win.py
Created February 3, 2014 19:32
Why have static type safety when we can roll it ourselves?
@postcondition(http_code_error)
def ptest_bauth(base_url=config['base_url'], action='post', user=config['puser'],
pw=config['ppw'], header=config['pauth_header'], token=''):
valid_actions = ['post', 'delete']
if action not in valid_actions:
assert False, "framework error: action is not in " + str(valid_actions)
@RobertFischer
RobertFischer / function_template.js
Created May 29, 2014 16:46
Demonstration of template function pattern
var function_template = function(method,cssClass,attr) {
return $(this.bulkUpdateEle)[method](cssClass).data(attr);
};
var foo_the_bars = _.partial(function_template, "foo", "bar");
var baz = _.partial(function_template, "baz");
foo({value1:true});
baz("quux", {value1:true});
foo({value2:false});
@RobertFischer
RobertFischer / uglifier.rb
Created June 27, 2014 12:36
Who needs static typing when we can roll our own?
# Starts on line 91 in uglifier-2.5.0
# Initialize new context for Uglifier with given options
#
# options - Hash of options to override Uglifier::DEFAULTS
def initialize(options = {})
(options.keys - DEFAULTS.keys - [:comments, :squeeze, :copyright])[0..1].each do |missing|
raise ArgumentError.new("Invalid option: #{missing}")
end
@RobertFischer
RobertFischer / check_this.js
Created July 8, 2014 18:04
JavaScript's hamstrung sanity checking
$(".foo").attr('bar', true);