Skip to content

Instantly share code, notes, and snippets.

class Hash
def map_values
h = {}; self.each_pair { |k,v| h[k] = yield(v) }; h
end
end
[ 'asdf', 'qwer' ].map{ |val| val * 2 }
# => [ 'asdfasdf', 'qwerqwer' ]
{ :one => 'asdf', :two => 'qwer' }.map{ |val| val * 2 }
@chrisjpowers
chrisjpowers / delayed_method_chain.rb
Created July 14, 2009 21:30
Allows you to declare alias_method_chain before either method has been defined.
# Allows you to declare an alias_method_chain before the original
# method has been defined.
#
# class A
# delayed_method_chain :name, :hello
#
# def name_with_hello
# "Hello #{name_without_hello}"
# end
#
@chrisjpowers
chrisjpowers / mock_console.js
Created April 13, 2010 21:49
Never have your javascript die from forgetting to take out console.log statements again!
// If console is not defined (ie the browser isn't
// using developer extensions), this will keep console
// statements accidentally left in the code from
// causing errors for the end user.
//
if(console == null) {
var console = {
log: function() {
// do nothing
}
function do_something(num, callback) {
// arbitrary logic here
num += 1;
// end arbitrary logic
return callback(num); // or you could do callback.call(this, num)
}
do_something(5, function(new_num) {
alert("After complex arbitrary logic, num is " + new_num);
});
module AutoAssetsHelper
# Outputs script and link tags for js/css based on
# the current action, controller and layout. This
# should be preferred over adding assets within a
# view file for specific views/layouts/controllers.
#
# Example: If UsersController#dashboard is requested
# and it uses the 'front' layout, these files will be
# included if they exist:
// This function returns a new function that will run
// the given function in the context of the given scope
// ie 'this' will be whatever you pass in as 'scope'
function scoped(fn, scope) {
return function () {
return fn.apply(scope, arguments);
}
}
var Obj = {
@chrisjpowers
chrisjpowers / crazy_chronic.rb
Created November 2, 2010 20:47
Strange Chronic date parsing, at least if today is 11/2/2010
require 'rubygems'
require 'chronic'
# This has historically worked, but today, not so much
Chronic.parse "next Monday 4:00am" #=> nil
# Though it works fine in the afternoon
Chronic.parse "next Monday 4:00pm" #=> Mon Nov 08 16:00:00 -0600 2010
# Doesn't work on Tuesday either
// Example taken from the CouchDB Pages app (though they used this inside a show page,
// while I'm using it on a list page -- could be part of the problem?).
//
// I get this same error any time that I return an object (rather than just a body string)
// inside a list function.
function(head, req) {
return {
code : 301,
headers : {
@chrisjpowers
chrisjpowers / wacky_undef.rb
Created May 24, 2011 14:16
Ruby undef vs. remove_method
class A
def hello
"hello from A"
end
end
class B
def hello
"hello from B"
end
@chrisjpowers
chrisjpowers / saving_without_callbacks.rb
Created July 6, 2011 21:42
Saving without callbacks - any better way?
module SavingWithoutCallbacks
def save_without_callbacks
raise "Only works for saved records" if new_record?
updates = {}
changes.each {|key, tuple| updates[key] = tuple.last}
self.class.update_all(updates, {:id => self.id}, {:limit => 1})
end
end
ActiveRecord::Base.send :include, SavingWithoutCallbacks