Skip to content

Instantly share code, notes, and snippets.

@ahoward ahoward/gist:31016
Created Dec 2, 2008

Embed
What would you like to do?
require 'cgi'
=begin
module CGI
def CGI.escape(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase
end
end
end
=end
class Hash
def query_string options = {}
escape = options.getopt(:escape, true)
pairs = []
esc = escape ? lambda{|v| CGI.escape v.to_s} : lambda{|v| v.to_s}
each do |key, values|
key = key.to_s
values = [values].flatten
values.each do |value|
value = value.to_s
if value.empty?
pairs << [ esc[key] ]
else
pairs << [ esc[key], esc[value] ].join('=')
end
end
end
pairs.replace pairs.sort_by{|pair| pair.size}
pairs.join('&')
end
def to_options!
replace to_options
end
def to_options
keys.inject(Hash.new){|h,k| h.update k.to_s.to_sym => fetch(k)}
end
def html_attributes
map{|k,v| [k, v.to_s.inspect].join('=')}.join(' ')
end
def getopt key, default = nil
[ key ].flatten.each do |key|
return fetch(key) if has_key?(key)
key = key.to_s
return fetch(key) if has_key?(key)
key = key.to_sym
return fetch(key) if has_key?(key)
end
default
end
def getopts *args
args.flatten.map{|arg| getopt arg}
end
def hasopt key, default = nil
[ key ].flatten.each do |key|
return true if has_key?(key)
key = key.to_s
return true if has_key?(key)
key = key.to_sym
return true if has_key?(key)
end
default
end
def hasopts *args
args.flatten.map{|arg| hasopt arg}
end
def delopt key, default = nil
[ key ].flatten.each do |key|
return delete(key) if has_key?(key)
key = key.to_s
return delete(key) if has_key?(key)
key = key.to_sym
return delete(key) if has_key?(key)
end
default
end
def delopts *args
args.flatten.map{|arg| delopt arg}
end
end
class String
def String.unindented! s
margin = nil
s.each do |line|
next if line =~ %r/^\s*$/
margin = line[%r/^\s*/] and break
end
s.gsub! %r/^#{ margin }/, "" if margin
margin ? s : nil
end
def String.unindented s
s = "#{ s }"
unindented! s
s
end
def unindented!
String.unindented! self
end
def unindented
String.unindented self
end
def String.indented! s, n = 2
margin = ' ' * Integer(n)
unindented!(s).gsub!(%r/^/, margin)
s
end
def String.indented s, n = 2
s = "#{ s }"
indented! s, n
s
end
def indented! n = 2
String.indented! self, n
end
def indented n = 2
String.indented self, n
end
def String.inlined! s
#s.gsub! %r/([^\n]|\A)\n(?!\n)/, '\1 '
s.strip!
s.gsub! %r/([^\n])\n(?!\n)/, '\1 '
end
def String.inlined s
s = "#{ s }"
inlined! s
s
end
def inlined!
String.inlined! self
end
def inlined
String.inlined self
end
def solid
gsub %r/[ ]/, '&nbsp;'
end
class Slug < ::String
def Slug.for *args
string = args.flatten.compact.join('-')
words = string.to_s.scan(%r/\w+/)
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
words.delete_if{|word| word.nil? or word.strip.empty?}
new words.join('-').downcase
end
end
def slug
Slug.for self
end
def / other
File.join self, other.to_s
end
end
class Array
def options
Hash === last ? pop : {}
end
end
class Object
def singleton_class &block
sc =
class << self
self
end
block ? sc.module_eval(&block) : sc
end
end
class Numeric
def parity
(self.to_i % 2) == 0 ? 'even' : 'odd'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.