Spox Library (splib)
The Spox Library is collection of helper methods and classes.
install (easy):
gem install splib
install (less easy):
git clone http://github.com/spox/splib.git
cd splib
gem build *.gemspec
gem install ./
install (less easy that’s a little easier)
rip makes it easy to install directly from a github repository.
Testing
This library has been tested on:
-
Ruby 1.8.6-p376
-
Ruby 1.8.7-p248
-
Ruby 1.9.1-p383
-
JRuby 1.4
Usage
The Spox Library has various things located within it. The Splib#load method will allow you to load individual parts of the library, or the entire thing into your program. Lets take a quick look at some of the things available from this library.
URL Shorteners
Access to a variety of URL shortening services
require 'splib'
Splib.load :UrlShorteners
puts Splib.tiny_url 'www.google.com'
puts Splib.trim_url 'www.google.com'
puts Splib.isgd_url 'www.google.com'
puts Splib.shortest_url 'www.google.com'
Results:
http://tinyurl.com/2ty
http://tr.im/Ig5f
http://is.gd/5wmJ1
http://tr.im/Ig5p
Conversions
Easy conversion for seconds and bytes to something more human readable
require 'splib'
Splib.load :Conversions
puts Splib.format_seconds 9999999
puts Splib.format_size 9999999999
Results:
3 months 3 weeks 1 day 17 hours 46 minutes 39 seconds
9.31 Gigabytes
Exec
Adding a bit of safety and ease of use to exec
require 'splib'
Splib.load :Exec
begin
Splib.exec('echo test', 10, 1)
rescue IOError
puts 'Exceeded allowed number of returned bytes'
end
begin
Splib.exec('while [ true ]; do true; done;', 1)
rescue Timeout::Error
puts 'Exceeded allowed running time'
end
puts Splib.exec('echo "hello world"')
Results:
Exceeded allowed number of returned bytes
Exceeded allowed running time
hello world
Constants
Find constants easily, especially within loaded modules
require 'splib'
Splib.load :Constants
mod = Module.new
mod.class_eval("
module Fu
class Bar
end
end"
)
p Splib.find_const('String')
fb = Splib.find_const('Fu::Bar', [mod]).new
p fb
p Splib.type_of?(fb, 'Fu::Bar')
Results:
String
#<#<Module:0x95f2fd0>::Fu::Bar:0x95f287c>
true
PriorityQueue
Add some logic to item queueing
require 'splib'
Splib.load :PriorityQueue
queue = Splib::PriorityQueue.new{|s| s == :last }
queue.push(:last, 'last')
2.times{ queue.push(:slot1, 'test') }
2.times{ queue.push(:slot2, 'fubar') }
until(queue.empty?)
puts queue.pop
end
Results:
test
fubar
test
fubar
last
Float
Need to know if your float is within a delta?
require 'splib'
Splib.load :Float
p 4.21.within_delta?(:expected => 4.30, :delta => 0.1)
=> true
Monitor
Annoyed that your monitor is generating thread owner errors? This simple monitor does its best to ensure your threads stay where they are supposed.
require 'splib'
Splib.load :Monitor
output = []
go = false
monitor = Splib::Monitor.new
t = Thread.new{ monitor.wait_until{go}; output << :foo}
Thread.new{ monitor.wait_while{!go}; output << :bar}
Thread.new{ monitor.wait; output << :foobar }
Thread.pass
monitor.broadcast
t.wakeup
Thread.pass
p output
go = true
monitor.signal
Thread.pass
p output
Thread.pass
monitor.broadcast
Thread.pass
p output
Results:
[:foobar]
[:foobar, :foo]
[:foobar, :foo, :bar]
TODO: Write examples for CodeReloader and RandomIterator
Last remarks
If you find any bugs, please report them through github. If you are in need of any help, you can generally find me on DALnet and Freenode.
License
Spox Library is licensed under the MIT License
Copyright (c) 2009 spox <[email protected]>