Skip to content

Instantly share code, notes, and snippets.

Action Pack
* #concat is now deprecated in favor of using <%= %> helpers [YK]
* Block helpers now return Strings, so you can use <%= form_for @foo do |f| %>.
<% form_for do |f| %> still works with deprecation notices [YK]
* Add a new #mount method on the router that does not anchor the PATH_INFO
at the end [YK & CL]
def morse s;26.times{|i|s.gsub!(/#{(97+i).chr}/i,%w[.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..][i])};s;end
require "test/unit"
class MaskTest < Test::Unit::TestCase
def test_morse
assert_equal ".-", morse("a")
assert_equal "-...", morse("b")
assert_equal "......-...-..--- .-----.-..-..-..", morse("Hello World")
end
end
def morse s;26.times{|i|s.gsub!(/#{(97+i).chr}/i,%w[.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..][i])};s;end
def luhn? n;s=0;n.size.times{|i|x=n[-(i+1),1].to_i*(i%2+1);s+=x>9?x+1:x};s%10==0;end
@ryanb
ryanb / morse.rb
Created June 10, 2010 17:56 — forked from swhitt/morse.rb
def morse s;26.times{|i|s.gsub! /#{(97+i).chr}/i,("$be5 J8G#T6P'&9Sl/,!-H0cfk"[i]-31).to_s(3).tr("12",".-")};s;end
require "test/unit"
require File.dirname(__FILE__) + '/business_hours'
class BusinessHoursTest < Test::Unit::TestCase
def setup
@hours = BusinessHours.new("8:00 AM", "5:00 PM")
end
def test_within_working_hours
assert_equal Time.parse("Dec 21, 2009 3:05 PM"), @hours.calculate_deadline(5*60, "Dec 21, 2009 3:00 PM")
module RailsAdmin
module AuthorizationAdapters
class CanCanAdapter
def filter_objects(objects)
objects.accessible_by(current_ability)
end
def authorize_action(action, class, object)
current_ability.authorize! action.to_sym, (object || class)
end
@ryanb
ryanb / deploy.rb
Created July 12, 2011 16:14
Check to see if you have changes in head which have not been pushed before deploying
namespace :deploy do
desc "Make sure there is something to deploy"
task :check_revision, :roles => :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
@ryanb
ryanb / rails_3_1_update.txt
Created July 23, 2011 21:31
Possible output from a Rails 3.1 upgrade wizard
$ rake rails:update
Generating app/assets directory and content. (same as 3.1 app generator)
Do you want to move all public/stylesheets into app/assets/stylesheets? (recommended) [y]
Moving public/stylesheets into app/assets/stylesheets...
Existing application.css, merging content to the end of app/assets/stylesheets/application.css.