Skip to content

Instantly share code, notes, and snippets.

@kch
kch / strip_html.rb
Created April 21, 2009 09:10
my super duper html stripper regexp
class String
def strip_html!
gsub!(/
<
\/? # optional end tag
([\w:-]+) # tag name (capturing)
(?: # optional attribute set (allowing even for end tags)
(?: # group for attribute repetition
\s+ # mandatory space before first attribute
[\w:-]+ # attribute name
@kch
kch / tracy.rb
Created April 25, 2009 01:56
dynamically define a method that takes a block in ruby 1.8; inspired by the pragprog metaprogramming screencasts
module Tracy
def self.included k
k.extend ClassMethods
end
module ClassMethods
def method_added m
wrap_method(m)
end
@kch
kch / cas-sql-authenticator-original.rb
Created May 14, 2009 06:10
UR DOIN' IT WRONG (untested)
class CASServer::Authenticators::SQL < CASServer::Authenticators::Base
def validate(credentials)
read_standard_credentials(credentials)
raise CASServer::AuthenticatorError, "Cannot validate credentials because the authenticator hasn't yet been configured" unless @options
raise CASServer::AuthenticatorError, "Invalid authenticator configuration!" unless @options[:database]
CASUser.establish_connection @options[:database]
CASUser.set_table_name @options[:user_table] || "users"
@kch
kch / think before you type.md
Created May 14, 2009 06:39
How to not waste lines and save kittens

So I ran into a bit of code that looked like this:

def my_method
  list.each do |item|
    return_value = item.some_property
    return return_value if return_value
  end
  nil
end
@kch
kch / modsux.rb
Created November 3, 2009 22:27
Assimilates git submodules' files, gets properly rid of the submodule metadata.
#!/usr/bin/env ruby
require 'yaml'
require 'pathname'
require 'shellwords'
module_url_by_path = Dir['**/.gitmodules'].inject({}) do |h, path|
IO.read(path).scan(/^\[submodule .*\].*\n((?:\s+.*\n)+)/).flatten.each do |s_attrs|
h_attrs = s_attrs.strip.split(/\n/).inject({}) do |h_attrs, s_attr_line|
k, v = s_attr_line.strip.split(/\s+=\s+/, 2)
h_attrs[k.to_sym] = v
#!/opt/local/bin/ruby1.9
require 'yaml'
urls = YAML::load <<-STR
http://foo.com/blah_blah : http://foo.com/blah_blah
http://foo.com/blah_blah/ : http://foo.com/blah_blah/
(Something like http://foo.com/blah_blah) : http://foo.com/blah_blah
http://foo.com/blah_blah_(wikipedia) : http://foo.com/blah_blah_(wikipedia)
http://foo.com/blah-blah-(wiki-pedia) : http://foo.com/blah-blah-(wiki-pedia)
@kch
kch / format number with separators.rb
Created November 29, 2009 22:33
format numbers with separators using a single regex substitution operation
require 'yaml'
RX = /(\d)(?=\d{3}+(?!\d))/
# Long version
RX_LONG = /
(\d) # One digit
(?= # That is followed by
\d{3}+ # One or more triples of digits
(?!\d) # That are not followed by a digit
@kch
kch / 0-tm-align-assignments.rb
Created December 19, 2009 12:33
fancier align assignments command for textmate
#!/usr/bin/env ruby1.9
# encoding: UTF-8
# Copyright © 2009 Caio Chassot
# Licensed under the WTFPL
def alternation(*s); s.map(&Regexp.method(:escape)).join("|") end
# All input is read here.
LINES = $stdin.readlines
IS_SELECTION = ENV.key?("TM_SELECTED_TEXT")
@kch
kch / filetrim.rb
Created January 1, 2010 23:57
removes trailing spaces on every line, leading and trailing empty lines; ensures files end with a line feed
#!/usr/bin/env ruby -pi
#
# Copyright © 2010 Caio Chassot
# Licensed under the WTFPL <http://sam.zoy.org/wtfpl/COPYING>
#
# File Trim. (Vertical and Right trim.)
# Command receives file paths as arguments. Modifies them in-place.
# - removes trailing spaces on every line;
# - removes leading and trailing empty lines;
# - ensures files end with a line feed.
@kch
kch / remove_entities.sh
Created January 2, 2010 19:38
decode all html entities in a project's files, in-place
$ ack --no-sql --ignore-dir=vendor -l '&\S+?;' | xargs \
ruby1.9 -rhtmlentities -pi -e '$_ = HTMLEntities.new.decode($_)'