Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby
# script/whitespace
#
# Strips whitespace from any files modified in git
# Also:
# - converts tabs to spaces
# - ensures a single newline at the end
class WhitespaceProcessor
def self.process(code)
class TestConstraint
def matches?(*args)
Rails.logger.info("TestConstraint")
end
end
TestApp::Application.routes.draw do
constraints(TestConstraint.new) do
resources :posts
end
class A
def protected_method
puts "success"
end
protected :protected_method
end
class B < A
def public_method
# Split unicode characters from supplementary planes (such as iOS 5 emoji) into surrogate pairs
# so that node.js can parse them.
def split_into_surrogate_pairs(text)
text.gsub(/[\u{10000}-\u{10ffff}]/) do |match|
codepoint = match.codepoints.first
pair = [0xD7C0 + (codepoint >> 10), 0xDC00 | codepoint & 0x3FF]
pair.pack("U*")
end
end
def test_method_explicit_return
raise "This should bubble up"
ensure
return "No problem here"
end
def test_method_implicit_return
raise "This should bubble up"
ensure
"No problem here"
@jpignata
jpignata / gist:1515215
Created December 23, 2011 19:59
Frees the links hidden in your public stream
#!/usr/bin/env ruby -rubygems
abort "Usage: links.rb USERNAME [days ago]" unless ARGV.length > 0
trap("SIGINT") { puts; abort }
require "twitter"
require "active_support/core_ext"
username = ARGV[0]
since = ARGV[1] ? ARGV[1].to_i.days.ago.to_date : 1.year.ago.to_date
@jpignata
jpignata / .vimrc
Created January 2, 2012 03:53
runs specs in first active terminal from vim
function! RSpec(args)
execute ":silent ! run-in-terminal.applescript 'rspec " . a:args . " %'"
endfunction
" command-R to run a focused spec
map <D-R> :call RSpec("-l " . <C-r>=line('.')<CR>)<CR>
" command-r to run all specs
map <D-r> :call RSpec("")<CR>
@jpignata
jpignata / gist:1653476
Created January 21, 2012 18:14
parses phone numbers to their locale via libphonenumber
// Parses a text file of numbers and outputs each number with its locale and
// counts of numbers found per each locale.
//
// Usage:
// scala -classpath ./lib/offline-geocoder-1.9.jar:./lib/libphonenumber-4.5.jar ./locator.scala <input_file>
import java.util.Locale
import scala.io.Source
import scala.collection.mutable.Map
import com.google.i18n.phonenumbers._
require "java"
class Item
include java.util.concurrent.Delayed
def initialize(deliver_at)
@deliver_at = deliver_at
end
def get_delay
require "java"
require "lib/vendor/libphonenumber-4.5.jar"
require "lib/vendor/offline-geocoder-1.9.jar"
class PhoneNumber
US_DIALING_PREFIX = "011"
US_COUNTRY_NAME = "US"
UNKNOWN = "Unknown"
import import java.util.Locale