Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / meta_method_definition_benchmark.rb
Created October 2, 2011 19:07
Benchmark comparing class_eval to define_method when defining methods
require "benchmark"
COUNT = 1000
Benchmark.bmbm do |test|
test.report "class eval" do
class ClassEvalClass;end
COUNT.times do |i|
ClassEvalClass.send :class_eval, "def method_no_#{i}; return #{i} ;end"
@Bodacious
Bodacious / modular.sass
Created October 7, 2011 12:53
Modular CSS Example
body{
/* styles that apply globally go here */
.hidden{
display: none;
visibility: hidden;
}
}
body.blog{
/* styles that only apply to blog pages go here
@Bodacious
Bodacious / navtabs.html
Created October 7, 2011 14:51
Flexible navigation tabs using CSS
<html>
<head>
<style type="text/css" media="screen">
#navbar {
display: table;
}
#navbar span {
display: table-cell;
text-align: center;
@Bodacious
Bodacious / application_helper.rb
Created October 19, 2011 13:46
jQuery Mobile Popup Windows
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"})
@Bodacious
Bodacious / inject.rb
Created November 16, 2011 09:22
Enumerable inject
string = ["b", "c", "d"].inject("a") { |result, element| result + element }
puts string
@Bodacious
Bodacious / .tm_properties
Created December 21, 2011 12:43
TextMate 2 Default Values
# ~/.tm_properties
softTabs = true
tabSize = 2
showInvisibles = true
fontName = 'Bitstream Vera Sans Mono'
fontSize = 20
spellingLanguage = 'en_GB'
@Bodacious
Bodacious / users_controller.rb
Created January 10, 2012 21:37
Build a RESTful API in Rails 3 Controller refactored: http://vimeo.com/user7965808/building-a-rest-api-in-rails-3
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
@Bodacious
Bodacious / application.html.erb
Created January 18, 2012 17:25
Adding a tag cloud to your Blogit blog
...
<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>
@Bodacious
Bodacious / README.md
Created January 24, 2012 20:29
New York Address generator

NY Address Generator

60% of the time it works EVERY time!

Installing

  gem 'ny_address', git: 'git://gist.github.com/1672379.git'
@Bodacious
Bodacious / datetime.rb
Created March 2, 2012 10:50
Custom date/time formats in Ruby on Rails
[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