Wednesday, May 30, 2007

Stupid Ruby Behaviors: Episode 1 (The Injury)

Because only "moon faced" kids like me would ever write a script that both read from STDIN and use ARGV

franz-macbook:~ mdfranz$ ruby moonface.rb
ddddddd
ddddddd
franz-macbook:~ mdfranz$ ruby moonface.rb me
moonface.rb:2:in `gets': No such file or directory - me (Errno::ENOENT)
from moonface.rb:2

mefranz-macbook:~ mdfranz$ cat moonface.rb
print ARGV
moonface = gets
print moonface

What gives? See gets documentation for the answer.

The solution is to use $stdin.gets

Tuesday, May 29, 2007

Nice Page: A Comparative Look at Some Ruby Idioms

Renaud Waldura has a nice page called A Comparative Look at Some Ruby Idioms that compares Java, Perl, and Ruby versions of stuff like
  • Constructors & Accessors
  • Function Pointers
  • Exception handling
  • Iterators
  • Variable Argument Lists
Useful because for many of us the some Ruby Ways are rather strange if you are coming from another language.

Monday, May 28, 2007

Unlocking Dark Secrets of rdoc/ri

So if you are not using the one-click installer (like anyone who compiles from source ) on Windows, ri/fxri is pretty useless. Not only don't your GEMs show, but anything else you've compiled doesn't either.

So how do you fix this?

Install it with rdoc -R from within the directory that has the .rb

# rdoc -R oniguruma.rb
oniguruma.rb: mcc..........c....c.....
Generating RI...

Files: 1
Classes: 4
Modules: 1
Methods: 19
Elapsed: 2.334s
Make sure it is there with ri -l

franz-g4:/my/src/oniguruma-1.1.0/lib root# ri -l | grep Oni
Oniguruma
Oniguruma::ORegexp
Oniguruma::ORegexp::escape
Oniguruma::ORegexp::last_match
Oniguruma::ORegexp::new
Oniguruma::ORegexp#==
Oniguruma::ORegexp#casefold?
Oniguruma::ORegexp#eql?
Oniguruma::ORegexp#inspect
Oniguruma::ORegexp#kcode
Oniguruma::ORegexp#options
Oniguruma::ORegexp#source
Oniguruma::ORegexp#to_s

On a related note segment7 has some nice blog entries on documentation

GSoC, RubyLand, or Finally Something Useful from Oreilly Ruby Blog

Instead of the normal vapid content like this, this announcement about RubyLand was actually sort of interesting as an example of solving small tasks using EventMachine and drb.
Rubyland’s goal is to simplify writing tiny scripts for small tasks. I’m planning to build it using a mix of dRB and EventMachine. Rubyland’s core will aggregate events from desktop applications, file system updates, other computers on the network, etc.. Rubyland will then call user-written Ruby scripts in response to those events. Initially Rubyland will target the Mac OS X platform in some ways, but its core will be cross-platform, so that plugins can be written for any platform.

Bravo!