Skip to content

Instantly share code, notes, and snippets.

@mudge
mudge / gist:124170
Created June 5, 2009 09:34
Add vim Ruby syntax highlighting to .prawn files.
au BufNewFile,BufRead *.prawn set filetype=ruby
@mudge
mudge / gist:130945
Created June 16, 2009 22:25
A solution to the ordering problem (http://gist.github.com/78519) in Scala.
def ordered(arrays: Seq[Any]*): Boolean =
arrays forall (array =>
arrays forall (array2 =>
Set(array : _*).intersect(Set(array2 : _*)).toSeq == Set(array2 : _*).intersect(Set(array : _*)).toSeq
)
)
@mudge
mudge / gist:161108
Created August 4, 2009 08:30
Map over a Ruby hash, replacing the values for each key.
# Map over a hash, replacing the values for each key.
x = {:a => 1, :b => 2}
#=> {:a => 1, :b => 2}
x.merge(x) { |k, v| v * 2 }
#=> {:a => 2, :b => 4}
# If you want to replace Enumerable's map...
class Hash
def map(&block)
merge(self, &block)
@mudge
mudge / gist:163332
Created August 6, 2009 14:19
A Ruby method to validate UK postcodes.
# Validate a UK postcode using a modified version of the official
# regular expression provided by
# http://www.govtalk.gov.uk/gdsc/schemaHtml/bs7666-v2-0-xsd-PostCodeType.htm
#
# @param [String] postcode the postcode to validate
# @return [Boolean] true if the postcode is valid, false if not
def is_valid_postcode?(postcode)
!!(postcode =~ /^\s*((GIR\s*0AA)|((([A-PR-UWYZ][0-9]{1,2})|(([A-PR-UWYZ][A-HK-Y][0-9]{1,2})|(([A-PR-UWYZ][0-9][A-HJKSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s*[0-9][ABD-HJLNP-UW-Z]{2}))\s*$/i)
end
@mudge
mudge / gist:191473
Created September 22, 2009 22:04
Unit test for a Sequel-using Sinatra application that rolls back DB changes on each test.
require 'sinatra_app'
require 'test/unit'
require 'rack/test'
set :environment, :test
class SinatraAppTest < Test::Unit::TestCase
include Rack::Test::Methods
# Make all tests transactional.
@mudge
mudge / gist:198862
Created October 1, 2009 09:44
Some JRuby to write an InputStream to a file.
# Write a Java stream to a file.
File.open('test.txt', 'w') do |f|
while (byte = stream.read) > -1
f << byte.chr
end
end
@mudge
mudge / gist:202182
Created October 5, 2009 15:21
Bash functions to switch between Ruby and JRuby.
function usejruby {
jrubypath=/opt/local/jruby/bin
PATH=$jrubypath:${PATH/$jrubypath:/}
}
function usecruby {
crubypath=/opt/local/ruby-enterprise/bin
PATH=$crubypath:${PATH/$crubypath:/}
}
@mudge
mudge / gist:203141
Created October 6, 2009 15:50
Pipe input into a system command in Ruby.
result = IO.popen(command, 'r+') do |io|
io.write(input)
io.close_write
io.read
end
@mudge
mudge / yajl.rb
Created October 29, 2009 10:59
A Rails initializer to use YAJL instead of ActiveSupport for JSON encoding.
# Use YAJL instead of ActiveSupport::JSON to encode objects to JSON in Rails.
class Hash
def to_json(options = nil)
Yajl::Encoder.encode(as_json(options))
end
end
class Array
def to_json(options = nil)
Yajl::Encoder.encode(as_json(options))
@mudge
mudge / gist:221616
Created October 29, 2009 17:17
A simple rich text editor with jQuery and iframe designMode.
/*
* A very basic rich text editor using jQuery and iframe designMode.
*
* In your HTML...
* <input type="hidden" id="article_body" name="article[body]">
* <iframe id="paste"></iframe>
*/
/*
* NOTE: The order of writing and setting designMode is very important.