Skip to content

Instantly share code, notes, and snippets.

@ashirazi
ashirazi / gist:4424177
Created January 1, 2013 00:33
String#strptime Ruby needs symmetry for Time#strftime. If a Time can be converted to a string (given a pattern), then a String should be convertible to a Time.
# This really should belong in String, not Date...
class String
# Calculate the Time represented by this string, and the (optional) time pattern
# that describes this string (see Time#strftime).
# Returns a nil time if the String does not match the pattern, or the pattern is invalid.
def strptime(format=nil)
date = format ? Date.strptime(self, format) : Date.strptime(self)
date.to_time
rescue
@xaviershay
xaviershay / application_controller.rb
Created May 19, 2011 21:43
Access helper methods outside controllers and views
class ApplicationController < ActionController::Base
# Provide access to helper methods from outside controllers and views,
# such as in Presenter objects. Rails provides ActionController::Base.helpers,
# but this does not include any of our application helpers.
def self.all_helpers
@all_helpers_proxy ||= begin
# Start with just the rails helpers. This is the same method used
# by ActionController::Base.helpers
proxy = ActionView::Base.new.extend(_helpers)