This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 : { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A | |
def hello | |
"hello from A" | |
end | |
end | |
class B | |
def hello | |
"hello from B" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer