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
# 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 | |
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
# "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 } |
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
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 |
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 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 |
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
class Analyzer | |
def initialize | |
@expansions = [] | |
@transformations = [] | |
@substitutions = {} | |
@tokenizer = lambda { |string| string.split } | |
end | |
def tokenizer(&proc) |
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
#!/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 |
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
# 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 | |
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
#!/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 |
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
# 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| |
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
# 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/*"] |
OlderNewer