x < lower ? lower : x > upper ? upper : x # Or should I just go this way: ...
Ruby Enforcing bounds
# Converts:
# {:hash => [{:foo => 1}, {:foo => 2}]}
#
...
Ruby Recursively convert multidi...
there has got to be a bette...
keys = {
"a" => %w(q w s z),
"b" => %w(v n g h),
...
Ruby Getting QWERTY misstypes
This is not really a refact...
def self.find_left_sibling( parent_id, cat_name )
parent = Category.find( :first,
:conditions => [ "id = ?", parent_id] )
...
Ruby Looking for alphabetized pl...
Set in array is in alpha or...
SUPPORTED_LANGUAGES = [["English", "en"], ["German", "de"]]
# request.env["HTTP_ACCEPT_LANGUAGE"] = "en-us,us;q=0.5"
@languages = request.env["HTTP_ACCEPT_LANGUAGE"].split(';')[0].split(',')
...
Ruby Accept Language Array
This is some rails code whe...
# given this structure:
array_of_hashes = [
{:callid => '1', :foo => '1', :created_at => '2007-09-21 11:45:15'},
...
Ruby Grouping an array of hashes
This chunk of code takes an...
# String#to_proc # # See http://weblog.raganwald.com/2007/10/stringtoproc.html ( Subscribe in a reader) ...
Ruby String#to_proc (by Reginald...
Reginald Braithwaite has a ...
require 'spec_statistics' ...
Ruby rake stats for RSpec
I want to get some statisti...
def new @course = Course.new ...
Ruby Using partials for a golf s...
I'm building a golf applica...
namespace :migrations do desc 'reverts, renames and re-executes uncommitted migrations that conflict with already committed migrations' ...
Ruby Rails migration conflict re...
Explained here: http://www....
def args_eval(line) tokens = [] while(data = line.match(/\[.*\]/)) ...
Ruby Argument eval
this code evaluates a strin...
has_attachment :content_type => :image ...
Ruby attachment_fu integration
I have used the attachment_...
require 'erb' class ClientMigrator def self.create(fields) ...
Ruby Dynamic migrations for rails
How do you create a rails p...
$c = []
def fork_reality(*l)
callcc {|c| $c << c }
...
Ruby Combinatorial explosion wit...
This code tries to find all...
class TreeNode
def TreeNode.action(action)
@@action = action
...
Ruby Changing contexts in a DSL ...
I use this code in a blog p...
def preview ...
Ruby Ajax form Preview
This is my first bit of Aja...
# Note: Kuwait does not use DST. # See how the utc_offset value represents ...
Ruby A DST-adjusted UTC offset
I need to get the Daylight-...
(1..10).each do |i| puts i end
Ruby Ruby simple loop
How to make this shorter ?
def random_pronouncable_password(size = 4) c = %w(b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr) v = %w(a e i o u y) ...
Ruby Random pronouncable password
Method that returns a rando...
?
Ruby Integer puzzle
If the integers from 1 to 9...
def parse_line
if line_is_parsable?
if line_is_nested?
...
Ruby Arrow Code
I hate arrow code. It's ug...
before_filter :login_required ...
Ruby restful_authentication role...
This is a couple of lines t...
def sum_string_sizes(units)
units.inject(0) {|memo, unit| memo + unit.size }
end
...
Ruby Sum string sizes
This code is functional, bu...
cap_growth = 5 v = (@property.current_value / 100 * cap_growth) + @property.current_value ...
Ruby compound interest
there must be a better way ...
# Behave like +link_to_remote+, but shows a spinner while the request is processing. # link_to_remote_with_spinner 'Hi', :url => hi_path # You can specify the spinner and the container to hide ...
Ruby link_to_remote_with_spinner
We use this utility interna...
Given a value and a range, ...