Skip to content

Instantly share code, notes, and snippets.

// sum an array, optionally providing a function to call on each element of the
// array to retrieve the value to sum
Array.prototype.sum = function(fn) {
return this.reduce(function(accum, elem) {
return accum + (fn ? fn(elem) : elem);
}, 0);
};
// flatten an array
// [1,2,[3,4]] -> [1,2,3,4]
# here's option one - standard way to add class methods and instance methods from a module into a class
module MyParent
# callback which is called on inclusion
def self.included(klass)
klass.extend(ClassMethods)
klass.send(:include, InstanceMethods)
end
# These are class methods added to klass
body{
/* styles that apply globally go here */
.hidden{
display: none;
visibility: hidden;
}
}
body.blog{
/* styles that only apply to blog pages go here
<html>
<head>
<style type="text/css" media="screen">
#navbar {
display: table;
}
#navbar span {
display: table-cell;
text-align: center;
module ApplicationHelper
# call this method in the view
# name should be a unique ID for this page, condition should be either truthy or falsey
def show_dialog_if(name, condition)
if condition
content =
link_to("click", "##{name}",
id: "#{name}_popup", class: "hidden",
data: {rel: "dialog", theme: "a"})
string = ["b", "c", "d"].inject("a") { |result, element| result + element }
puts string
class UsersController < ApplicationController
# This can be moved to ApplicationController if
# every action within the application should respond to these MIME Types
respond_to :html, :json, :xml
def index
respond_with users
end

NY Address Generator

60% of the time it works EVERY time!

Installing

  gem 'ny_address', git: 'git://gist.github.com/1672379.git'
...
<aside id="right">
<nav id="tag_cloud">
<h3>Browse by topic:</h3>
<% tag_cloud(tags, %w(tag1 tag2 tag3 tag4 tag5 tag6)) do |tag, tag_class| %>
<%= link_to tag.name, tagged_blog_posts_path(tag: tag.name), :class => "tag #{tag_class}" %>
<% end %>
</nav>
</aside>
[Time, Date].map do |klass|
klass::DATE_FORMATS[:variable] = lambda do |t|
date =
case
# display today's date as "today"
when t >= Date.today then "today"
# display yesterday's date as 'Yesterday'
when (t >= Date.yesterday and t <= Date.today) then "yesterday"
# otherwise, display date: eg. Mon 4th June
else