Skip to content

Instantly share code, notes, and snippets.

@automatthew
automatthew / gist:569
Created July 22, 2008 01:22 — forked from dyoder/domain.rb
domain name class; QUESTIONABLE
# first we will give Domains a little help
class Domain < String
def subdomain( rank = 1 ) ; self.split('.')[0..(rank-1)].join('.') ; end
def level( rank ) ; self.split('.')[(rank*-1)..-1].join('.') ; end
def top_level ; level(1) ; end
end
# now we can treat them like strings but also access domain components naturally
@automatthew
automatthew / methodizer.rb
Created October 14, 2008 20:08
Distinguish origin of Ruby methods
# "local" methods are those defined directly on a constant,
# as opposed to those inherited or mixed in
class Module
def local_class_methods
self.methods.select { |m| self.method(m).owner == self }
end
def local_instance_methods
self.instance_methods.select { |m| self.instance_method(m).owner == self }
@automatthew
automatthew / README
Created February 4, 2009 22:18
DEPRECATED: dollarspots owns your loadpath
Stop putting "require 'rubygems'" in your project files. Just madly require
away, assuming that whatever you need will be found in the load path.
Install dollarspots.rb early in your loadpath. It checks the pwd for a file
named .dollarspots.rb and loads it if found. Use .dollarspots.rb files locally
in projects to jigger with your loadpath such that your reckless requires
actually work. Note that this may involve saying "require 'rubygems'".
Call ruby with -rdollarspots, or set "rdollarspots" as your RUBYOPT.
To get TextMate's RubyMate facilities to work you'll need to edit the RUBYOPT
@automatthew
automatthew / thread_safe_camel_snake.rb
Created April 8, 2009 14:03
camel and snake casing
def camel_case( string )
string.split('_').map do |word|
"#{word.slice(/^\w/).upcase}#{word.slice(/^\w(\w+)/, 1)}"
end.join
end
def snake_case( string )
string.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
end
@automatthew
automatthew / analyzer.rb
Created July 23, 2009 20:53
token analysis and expansion
class Analyzer
def initialize
@expansions = []
@transformations = []
@substitutions = {}
@tokenizer = lambda { |string| string.split }
end
def tokenizer(&proc)
@automatthew
automatthew / n-log.rb
Created August 14, 2009 18:28
like ngrams, but for phrases
#!/usr/bin/env ruby
require 'enumerator'
max = ARGV[0].to_i || 5
while string = STDIN.gets
bits = string.chomp.split
(2..max).each do |i|
bits.each_cons(i) do |nlog|
puts nlog.join(" ")
end
@automatthew
automatthew / edits.rb
Created August 14, 2009 19:48
basic edits for Strings
# useful for things like http://norvig.com/spell-correct.html
module Edits
DICT = { "cap" => 1, "carp" => 1, "clap" => 1, "cramp" => 1 }
def deletes
map_transforms { |word, i| word.delete_at(i) }
end
@automatthew
automatthew / script.rb
Created September 29, 2009 16:16
Pattern for single file lib/script
#!/usr/bin/env ruby
#
# Simple pattern for a Ruby script where the command line processing
# is done separately from the rest of the code. This allows the file
# to be required by other Ruby code without acting as an executable.
class Project
# Defaults to be overridden by command line options
@automatthew
automatthew / solr_tomcat.rake
Created October 15, 2009 21:52
rake tasks for Solr
# This task presumes you have made an erb template of solrconfig.xml
# with an interpolation like this:
# <dataDir>${solr.data.dir:<%= app_root %>/solr/data}</dataDir>
desc "generate solrconfig.xml file for ./solr_app"
task :configure => "solr_app/conf/solrconfig.xml"
rule '.xml' => '.erb' do |t|
require 'erubis'
File.open(t.name, "w") do |f|
@automatthew
automatthew / instant.rake
Created November 10, 2009 20:13
compile and run trivial Java with Rake
# instant.rake
# Rake rules for compiling and running trivial Java programs
#
# Usage: rake com.example.MonkeyShines
# Source goes under ./src
# Classes end up under ./target
require 'rake/clean'
libs = FileList["lib/*"]