Takahashi Method
Eric Hodel | Tue, 04 Jul 2006 07:16:00 GMT
M. 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!
—Daniel Berger, ruby-talk 200090
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.
Care and Feeding of Timeout.timeout
Eric Hodel | Tue, 11 Apr 2006 17:04:00 GMT
If you’re not careful when using Timeout.timeout you can end up with some hard to find bugs.
The first is that nesting timeouts without different timeout exception classes is very bad. Let’s say you have a process that can connect to multiple servers, but you want to give up and try the next server if it takes too long. You’d probably write something like this:
require 'timeout'
servers = [1, 2]
current_server = 0
begin
Timeout.timeout 2 do
puts "Connecting to server #{servers[current_server]}"
sleep # simulate work
end
rescue Timeout::Error
puts "Failed"
current_server += 1
retry unless current_server == servers.length
end
This is sensible code, if the work takes to long you’ll fail and move on to the next server.
But now its been a few months and you’ve added servers, but you want your application to only try for so many seconds then give up completely. Your real app would be properly factored (of course) so the bug with the simple solution wouldn’t necessarily be obvious:
require 'timeout'
servers = [1, 2]
current_server = 0
Timeout.timeout 5 do
puts 'Setting up some stuff'
sleep 4
begin
Timeout.timeout 2 do
puts "Connecting to server #{servers[current_server]}"
sleep
end
rescue Timeout::Error
puts "Failed."
current_server += 1
retry unless current_server == servers.length
end
end
When we run this code we run longer than we were supposed to:
<samp>$ time ruby t.rb Setting up some stuff Connecting to server 1 Failed. Connecting to server 2 Failed. real 0m7.044s user 0m0.014s sys 0m0.011s
Why seven seconds instead of five? The inner timeout block caught the outer timeout block’s exception and continued doing what it was doing. This isn’t what we want, but Timeout.timeout allows you to change the raised exception:
require 'timeout'
servers = [1, 2]
current_server = 0
class ServerTimeout < Timeout::Error; end
class AppTimeout < Timeout::Error; end
Timeout.timeout 5, AppTimeout do
puts 'Setting up some stuff'
sleep 4
begin
Timeout.timeout 2, ServerTimeout do
puts "Connecting to server #{servers[current_server]}"
sleep
end
rescue ServerTimeout
puts "Failed."
current_server += 1
retry unless current_server == servers.length
end
end
So now the outer timeout can stop execution even from inside the inner timeout:
<samp>$ time ruby t.rb
Setting up some stuff
Connecting to server 1
/usr/local/lib/ruby/1.8/timeout.rb:54: execution expired (AppTimeout)
from /usr/local/lib/ruby/1.8/timeout.rb:56:in `timeout'
from t.rb:13
from /usr/local/lib/ruby/1.8/timeout.rb:56:in `timeout'
from t.rb:9
real 0m5.069s
user 0m0.022s
sys 0m0.015s</samp>
The second to watch out for is Timeout killing your rescue or ensure blocks. A timeout raised inside an ensure block will stop execution, so for critical ensure blocks you should wrap them in their own begin/end block:
require 'timeout'
Timeout.timeout 2 do
timeout = nil
begin
puts "Allocating the thingy..."
sleep 1
raise RuntimeError, 'Oh no! Something went wrong!'
ensure
# Since we might time out, hold onto the timeout we caught
# so we can re-raise it when we're done cleaning up.
begin # we really need to clean up
puts "Cleaning up after the thingy..."
sleep 2
puts "Cleaned up after the thingy!"
rescue Timeout::Error => e
puts "Timed out! Trying again!"
timeout = e # save that timeout then retry
retry
end
# Raise the timeout so we time out all the way to the top.
raise timeout unless timeout.nil?
end
end
Ruby 2.0 Features
Eric Hodel | Tue, 11 Apr 2006 00:04:45 GMT
17:01 < branstrom> blink: Ruby 2.0 will make you coffee too 17:01 < ne78> will Ruby 2.0 cook my meal ? 17:01 < nome> ne78: no, but it will get you a gf who will 17:01 < ne78> nome: no thanks i already have one 17:01 < nome> ne78: this one will be able to cook
Chasing "undefined method for Fixnum"
Eric Hodel | Fri, 07 Apr 2006 17:28:35 GMT
I’ve been running the unit tests for 43 People on my Powerbook since the shared development box slowed to a crawl after importing the entire production database and I’m now seeing the “undefined method [blah] for -517611318:Fixnum” message occasionally on my Powerbook. Not content to suffer through running another thirty seconds of tests I reached into eval.c and added a call to rb_bug() when method_missing is called on the Fixnum -517611318.
Within a couple runs of the tests I ended up with a core, but the NODE * argument to rb_eval() was 0×0 for all calls. This made no sense, that value can’t possibly be 0×0 for every call to rb_eval, the optimization flags must have been getting in the way.
So I rebuilt Ruby with -O0 and haven’t gotten a core since. So one of the optimizations that -O or -O2 makes is screwing up Ruby’s internals. Now I’m trying a -O1 build to see if I can reproduce. If I don’t get any problems with -O1 I’ll try sorting through -O2’s flags until I find the culprit.

Articles