Setting $stdout per-thread
Eric Hodel | Wed, 16 Aug 2006 18:58:00 GMT
cdfh on #ruby-lang asked how to redirect $stdout per-thread and I came up with this solution, redirect via a thread-local variable:##
# Allows $stdout to be set via Thread.current[:stdout] per thread.
module ThreadOut
##
# Writes to Thread.current[:stdout] instead of STDOUT if the thread local is
# set.
def self.write(stuff)
if Thread.current[:stdout] then
Thread.current[:stdout].write stuff
else
STDOUT.write stuff
end
end
end
$stdout = ThreadOut
Simple test:
require 'stringio'
require 'threadout'
s = StringIO.new
Thread.start do
Thread.current[:stdout] = s
puts 'redirected to StringIO'
end.join
Thread.start do
puts 'no redirection'
end.join
puts s.string
Output:
no redirection redirected to StringIO
RDoc Mega Update
Eric Hodel | Sat, 05 Aug 2006 05:08:56 GMT
With many thanks to Hugh Sasse for doing much of the gruntwork, there will be a huge increase in RDoc available in Ruby 1.8.5. If you have the time, get the latest from CVS (HEAD or ruby_1_8) and make install install-doc, check it out, and report back anything busted, poorly worded, or in need of general cleanup. I have until Sunday night to fix anything you find.Ruby to JavaScript
Eric Hodel | Wed, 05 Jul 2006 21:35:04 GMT
Paul Battley wrote on the ruby-talk mailing list:This is a pretty nifty use of ParseTree, hopefully I'll get him using ParseTree's SexpProcessor, too. UPDATE: The Convert Ruby to JavaScript homepage.Here's something for the hungry mob to tear apart: automatic Ruby to JavaScript conversion. It seems somehow wrong, like grafting a pretty girl's head onto a donkey, but I've done it anyway!
[...]
It uses Ryan Davis et al's ParseTree and Florian Gro's ruby.js for most of the hard work. It's still very limited, and there are many warts, not least of which is the requirement for an explicit receiver on every method.
That said, the code's here: http://po-ru.com/files/ruby2js.tar.gz
Takahashi Method
Eric Hodel | Tue, 04 Jul 2006 07:16:00 GMT
-- Daniel Berger, ruby-talk 200090M. Edward (Ed) Borasky wrote: > Pawel Szymczykowski wrote: > > On 7/3/06, James Britt wrote: > > > Thanks for pointing this out. I recall when this was > > > first suggested, and I'm glad it just faded away. > > > > Pfft.. what are the kids all suposed to go out and get > > tattoos of then? > > Celtic symbols. No, Japanese characters. That way yo can use the Takahashi method to give a presentation simply by removing your clothing!
OS X 10.4.7 and /usr/bin/ruby
Eric Hodel | Wed, 28 Jun 2006 16:37:36 GMT
Beware! OS X 10.4.7 reinstalls /usr/bin/ruby, so depending upon how you've replaced Apple's install of ruby you may start running 1.8.2 unexpectedly! By the way, if you're running a hand-built ruby on OS X, build with -O instead of -O2, you won't get that annoying undefined method for Fixnum exception.Ruby 1.8.5p1 Released
Eric Hodel | Sat, 24 Jun 2006 10:21:00 GMT
Ruby 1.8.5 preview 1 is available for download.
There's a big, big change in 1.8.5p1, ri will now read documentation from gems. I'd really like to get feedback on this in case any unexpected bugs show up. To test this out you'll need to install 1.8.5p1 and grab the latest rubygems:
gem update --system --source http://onestepback.org/betagems
Then run:
gem rdoc --ri --no-rdoc gemname
For example:
$ sudo gem rdoc --ri --no-rdoc rc-rest Installing ri documentation for rc-rest-1.0.0...
You should see many more classes in ri -l:
$ ri -l | egrep -v '::[a-z]|#' ... RCRest RCRest::Error Autotest RailsAutotest UnitDiff ZenTest
(Hrm, maybe I should add sorting to ri -l.)
I also made the rdoc command not print warnings when being run in quiet mode so newbies won't get scared when the gem command spits out strange messages.
Geocoding Goodness
Eric Hodel | Tue, 13 Jun 2006 22:01:00 GMT
I've implemented ruby bindings for the three major geocoding APIs as gems:
| API | gem name |
|---|---|
| Yahoo! | yahoo-geocode |
| google-geocode | |
| geocoder.us | geocoder-us |
And as a bonus, you get a Yahoo! search gem to search either the web or for locations, yahoo-search.
The bindings all have similar APIs, but return different objects depending upon what information the API reliably provides. Yahoo! gives the most information, followed by Google, followed by geocoder.us.
Yahoo!
require 'rubygems'
require 'yahoo/geocode'
yg = Yahoo::Geocode.new application_id
locations = yg.locate '701 First Street, Sunnyvale, CA'
p location.first.coordinates
(and searching for locations)
require 'rubygems'
require 'yahoo/local_search'
yls = Yahoo::LocalSearch.new application_id
results, = yls.locate 'pizza', 94306, 2
results.each do |location|
puts "#{location.title} at #{location.address}, #{location.city}"
end
require 'rubygems'
require 'google_geocode'
gg = GoogleGeocode.new application_id
location = gg.locate '1600 Amphitheater Pkwy, Mountain View, CA'
p location.coordinates
geocoder.us
documentationrequire 'rubygems'
require 'geocoder_us'
gu = GeocoderUs.new 'username', 'password'
p gu.locate('1924 E Denny Way, Seattle, WA')
Marshal.load Speed
Eric Hodel | Wed, 03 May 2006 04:52:41 GMT
File.open 'dump' do |fp| Marshal.load fp end
Is much slower than:
File.open 'dump' do |fp| Marshal.load fp.read end
Because the former uses IO#getc and the latter operates directly on a String.
I learned this tonight while helping profile code at a Seattle.rb hacking night.
DRb an Introduction and Overview
Eric Hodel | Sun, 23 Apr 2006 00:57:34 GMT
I just completed my presentation on Distributed Ruby an Introduction and Overview at the Silicon Valley Ruby Conference. Free of charge you can download a PDF of the slides (720k) or the Keynote original (544k, Apple Keynote only).
Now that I have two outlines of more DRb functionality I should seriously consider writing a book on it.
Update on "undefined method for Fixnum"
Eric Hodel | Thu, 20 Apr 2006 18:37:44 GMT
A week or so ago I managed to chase down the “undefined method for Fixnum” bug to the compiler flags -g -O1 -pipe -fno-common -DRUBY_EXPORT -fschedule-insns2. For now you can get rid of the bug by compiling -O1 instead of switching to gcc3. I haven’t had time to track it down further, mostly because I ran out of time and the man page doesn’t specify what -O1 really contains.
Hopefully some smarter eyeballs than mine can jump on this and figure out if GCC is wrong or if Ruby is missing a volatile somewhere.

Articles