This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
au BufNewFile,BufRead *.prawn set filetype=ruby |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ordered(arrays: Seq[Any]*): Boolean = | |
arrays forall (array => | |
arrays forall (array2 => | |
Set(array : _*).intersect(Set(array2 : _*)).toSeq == Set(array2 : _*).intersect(Set(array : _*)).toSeq | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Write a Java stream to a file. | |
File.open('test.txt', 'w') do |f| | |
while (byte = stream.read) > -1 | |
f << byte.chr | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function usejruby { | |
jrubypath=/opt/local/jruby/bin | |
PATH=$jrubypath:${PATH/$jrubypath:/} | |
} | |
function usecruby { | |
crubypath=/opt/local/ruby-enterprise/bin | |
PATH=$crubypath:${PATH/$crubypath:/} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
result = IO.popen(command, 'r+') do |io| | |
io.write(input) | |
io.close_write | |
io.read | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. |