Skip to content

Instantly share code, notes, and snippets.

View sporkmonger's full-sized avatar

Bob Aman sporkmonger

View GitHub Profile
##
# Sets the query component for this URI.
#
# @param [String, #to_str] new_query The new query component.
def query=(new_query)
@query = new_query ? new_query.to_str : nil
# Reset dependant values
@normalized_query = nil
end
@sporkmonger
sporkmonger / gist:31013
Created December 2, 2008 05:28
This is NOT how to generate valid UUIDs.
def rand_hex_3(l)
"%0#{l}x" % rand(1 << l*4)
end
def rand_uuid
[8,4,4,4,12].map {|n| rand_hex_3(n)}.join('-')
end
UUID # a wrapper class for UUIDs, not a generator, with handy implicit to_str cast
UUID::Generator
UUID::TimestampGenerator.new
UUID::RandomGenerator.new
UUID::SHA1Generator.new(namespace)
UUID::MD5Generator.new(namespace)
UUID::Generator::DEFAULT = UUID::TimestampGenerator.new
uuid = UUID.new
#=> #<UUID:0xdb5268 UUID:012c377a-bfeb-11dd-b84a-001ec2186a45>
$ SYDNEY=1 bin/rbx gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.1
- RUBY VERSION: 1.8.6 (12/31/2009 patchlevel 111) [i686-apple-darwin9.5.0]
- INSTALLATION DIRECTORY: /Users/sporkmonger/Projects/Ruby/OpenSource/rubinius/gems/1.8
- RUBYGEMS PREFIX: /Users/sporkmonger/Projects/Ruby/OpenSource/rubinius
- RUBY EXECUTABLE: /Users/sporkmonger/Projects/Ruby/OpenSource/rubinius/bin/rbx
- EXECUTABLE DIRECTORY: /Users/sporkmonger/Projects/Ruby/OpenSource/rubinius/bin
- RUBYGEMS PLATFORMS:
- ruby
# http://github.com/sporkmonger/retrieve/tree/master
require "retrieve/clients/http"
connections = {}
100.times do
Retrieve.open("http://google.com/", :connections => connections) do |resource|
puts resource.read[0..75] + "..."
end
end
require "addressable/uri"
Addressable::URI.parse(
 "http://example.com/a/b/c/?one=1&two=2#foo"
).extract_mapping(
 "http://{host}/{-suffix|/|segments}?{-join|&|one,two,bogus}\#{fragment}"
)
#=> {
 "host" => "example.com",
 "segments" => ["a", "b", "c"],
 "one" => "1",
@sporkmonger
sporkmonger / gist:25325
Created November 15, 2008 20:53
Split Weirdness
# WHY?
"/foo/bar/baz".split("/")
# => ["", "foo", "bar", "baz"]
"/foo/bar/baz/".split("/")
# => ["", "foo", "bar", "baz"]
"foo/bar/baz/".split("/")
# => ["foo", "bar", "baz"]
# What's the best way to get this result?
# Percent encodes a URI or component.
#
# @param [String, #to_str] uri The URI or component to encode.
#
# @option [String, Regexp] character_class
# The characters which are not percent encoded. If a <tt>String</tt>
# is passed, the <tt>String</tt> must be formatted as a regular
# expression character class. (Do not include the surrounding square
# brackets.) For example, <tt>"b-zB-Z0-9"</tt> would cause everything
# but the letters 'b' through 'z' and the numbers '0' through '9' to be
@sporkmonger
sporkmonger / gist:20866
Created October 30, 2008 00:20
mate_gem
#!/usr/bin/env ruby
gem_home = `gem env home`
gem_repository = File.join(gem_home, "gems")
gem_name = ARGV.shift
if gem_name == nil || gem_name == ""
puts "Must supply a gem name."
exit(1)