Skip to content

Instantly share code, notes, and snippets.

@josevalim
josevalim / _yerb.rb
Created March 18, 2010 16:41
Who needs HAML when you have YAML + ERB?
# = YERB
#
# Who needs HAML when you have YAML + ERB? ;)
#
# See example.yaml below for an example. You can run this code
# by cloning this gist and then `ruby _yerb.rb example.yaml`.
#
# Notice that you need Ruby 1.9 so the hash order is preserved.
# Obviously, this is just for fun. Definitely slow as hell.
#
@moro
moro / gist:1024620
Created June 14, 2011 10:05
Kaminari and pushState
(function($){
$('.pagination a[data-remote=true]').live('ajax:success', function(e){ window.history.pushState('', '', $(e.target).attr('href')) })
$(window).bind('popstate', function(){ $.ajax({url:window.location, dataType:'script'}) ; return true });
})(jQuery);
@mudge
mudge / maybe.rb
Last active October 2, 2015 20:27
Implementing Enumerable to simulate Monads
class Maybe
include Enumerable
def initialize(value)
@value = value
end
def each
yield value unless value.nil?
end
@ptaoussanis
ptaoussanis / smart_memoize.clj
Created June 14, 2012 11:19
More powerful memoize for Clojure
(def ^:private gc-sm-cache!
"Maintains maximum cache size by intelligently pruning less valuable items."
(let [gc-running? (atom false)]
(fn [cache ttl max-items now]
(when-not @gc-running?
(reset! gc-running? true)
(let [snapshot @cache
@lengarvey
lengarvey / _do_flash.html.haml
Last active December 16, 2015 01:08
Zurb Foundation Rails Flash partial
- flashes = {:notice => 'success', :alert => 'standard', :error => 'alert', :secondary => 'secondary'}
- flashes.each do |key, value|
- if flash[key]
.alert-box{:class => value, :data => { :alert => '' } }
= flash[key]
= link_to '×'.html_safe, '#', :class => 'close'
@JEG2
JEG2 / struct.md
Created June 3, 2013 21:50
Thinking out loud about the merits and drawbacks of two different usages of Struct…

How Should We Use Struct?

The Choice

It's common in Ruby to see some code setup a Struct like this:

class Specialized < Struct.new(:whatever)
  # ... define custom methods here...
end
@brycelambert
brycelambert / gist:7252506
Created October 31, 2013 16:17
javascript course app
var CourseApp = {
students: [],
courses: [],
teachers: [],
render_item: function(item, type) {
var html = this.render_html(item, type);
document.getElementById(type + "-list").innerHTML += html;
},
render_items: function(items_name) {
var items = this[items_name];

##Running a Gem from Source

In your Gemfile, specify the gem and the path to its source.

gem 'gem_name', path: '/~/code/gem_folder'

Require the gem in your program.

def values(hsh, key)
return [] if !hsh.kind_of? Hash
v = hsh[key] ? [hsh[key]] : []
hsh.values.select{|i| i.kind_of? Hash or i.kind_of? Array}.each do |val|
if val.kind_of? Hash
v+= values(val, key)
else
val.each {|i| v+= values(i, key)}
end
end