Skip to content

Instantly share code, notes, and snippets.

View mcmire's full-sized avatar
🎯
Not a lot of time for side projects these days, so ping me if I'm unresponsive!

Elliot Winkler mcmire

🎯
Not a lot of time for side projects these days, so ping me if I'm unresponsive!
View GitHub Profile
@mcmire
mcmire / alternate_constructor_pattern.coffee
Last active August 29, 2015 13:57
Design patterns: JavaScript vs. Ruby
# If you wanted to make a constructor that did some extra stuff
# on the newly created object before giving it back to you
# you might be tempted to write it like you would in Ruby:
class Modal
@load: ->
modal = new this
modal.load
modal
@mcmire
mcmire / limited_exposure.rb
Last active August 29, 2015 14:04
Like DecentExposure, but less magical
# This is a module that provides two methods, `expose` and `let`, which
# allow you to cleanly and clearly define lazily-evaluated, memoized
# values that you can use anywhere in your controllers and views. It's
# very similar to the decent_exposure[1] gem, but it works more simply
# (and doesn't have a default action when you don't pass a block to
# #expose, to keep things very very simple).
#
# You will want to mix this into ApplicationController.
#
# == Rationale
@mcmire
mcmire / single_or_multiple.rb
Created May 28, 2015 16:59
SingleOrMultiple functor (Ruby)
require "attr_extras"
class SingleOrMultiple
rattr_initialize :value
def map(&block)
result =
if multiple_values?
value.map(&block)
else

CoffeeScript: Differences from JavaScript

Semicolons

Don't write them; they'll be inserted automatically.

Functions

Replace function () { ... } with ->.

getClientLocation = ->
new Promise (resolve, reject) ->
success = (position) ->
resolve(position.coords)
error = (positionError) ->
error = new Error("Error fetching client location: " + positionError.message)
reject(error)
navigator.geolocation.getCurrentPosition(success, error)
@mcmire
mcmire / gist:49602
Created January 20, 2009 18:58
Extending Prototype to retain order of form data when sending an Ajax request
Object.extend(Object, {
clone: function(object) {
return object.clone ? object.clone() : Object.extend({ }, object);
},
toQueryString: function(object) {
return object.toQueryString ? object.toQueryString() : $H(object).toQueryString()
}
});
var PseudoHash = Class.create(Hash, (function() {
@mcmire
mcmire / Improving not_a_mock.rb
Created February 28, 2009 23:12
Various ideas for improving not_a_mock so that we don't have to track methods manually
# Various ideas for improving not_a_mock so that we don't have to track methods manually
# Note that none of these work, so don't try them
class Object
def track_all_methods
ancestors = self.class.ancestors - [Object, Kernel, ...] # insert all of Ruby Core classes here
instance_methods = ancestors.map {|x| x.instance_methods(false) }
class_methods = ancestors.map {|x| x.singleton_methods(false) }
methods = (instance_methods + class_methods).flatten.map {|x| x.to_sym }
track_methods(*methods)
@mcmire
mcmire / YAML::Omap fix in Ruby 1.8.7
Created April 10, 2009 21:32
YAML::Omap fixes for Ruby 1.8.6
#!/usr/bin/env ruby
#
# In Ruby 1.8.6, there's a bug in YAML that is revealed when you
# serialize and then deserialize a large YAML::Omap. If you look
# at the generated YAML, it looks like every so often, instead
# of an object showing up in the YAML file, the object is skipped
# and another a reference to another object shows up. This does
# not seem to happen with an Array of values, or an Array of Arrays,
# or an Array of Hashes, or a Hash. This behavior was fixed in 1.8.7.
#
@mcmire
mcmire / actionmailer_layout_patch.rb
Created May 7, 2009 15:35
ActionMailer plain text layout fix
# Patch Rails so that a plain text mailer view won't be rendered with the layout
# Only tested in Rails 2.2 - I'm not sure how this works with 2.3
module ActionMailer
class Base
def candidate_for_layout?(options)
return false if ActionView::Template === options[:file] && options[:file].content_type == "text/plain"
!@template.send(:_exempt_from_layout?, default_template_name)
end
end
end
@mcmire
mcmire / powergrep.pl
Created May 14, 2009 15:34
powergrep -- improved rgrep with colors and the ability to omit directories
#!/usr/bin/perl
#================================================================================
# powergrep -- improved rgrep with colors and the ability to omit directories
#================================================================================
# Created: 6 Sep 2007
# Last modified: 2 Jun 2009
# (c) 2007-2009 Elliot Winkler
#================================================================================
# CHANGELOG:
# * 2 Jun 2009 - added --mate option to open matched files in TextMate