Haven't blogged here forever (mostly using Python these days, although I did break into some JRuby a couple of weeks back to avoid Java coding) but definitely check out 21 Ruby Tricks from Peter Cooper.
Regex, Array Joins, file operations, and more
Good stuff..
Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts
Monday, May 26, 2008
Sunday, August 5, 2007
Debian + FXRI + Rubygems is Englightenment!

So unlike OSX FXRI on Debian (Etch) is a snap. This assumes you are running a self compiled 1.8.6 install with gems. All I had to do was apt-get install the following packages:
All you really need is the *-dev of course. Which resulted in the following additional packages being installed:
The following NEW packages will be installed:
libcupsys2-dev libexpat1-dev libfontconfig1-dev libfox-1.6-0 libfox-1.6-dev
libfox-1.6-doc libfreetype6-dev libgcrypt11-dev libgnutls-dev
libgpg-error-dev libjpeg62-dev liblzo-dev libopencdk8-dev libpng12-dev
libpopt-dev libtasn1-3-dev libtiff4-dev libtiffxx0c2 libxcursor-dev
libxfixes-dev libxft-dev libxrender-dev x11proto-fixes-dev
x11proto-render-dev
And then
root@franz-t61:~/Desktop/Downloads/ruby-1.8.6# gem install fxri
Need to update 3 gems from http://gems.rubyforge.org
...
complete
Install required dependency fxruby? [Yn] Y
Select which gem to install for your platform (i686-linux)
1. fxruby 1.6.11 (ruby)
2. fxruby 1.6.11 (mswin32)
3. fxruby 1.6.10 (mswin32)
4. fxruby 1.6.10 (ruby)
5. Skip this gem
6. Cancel installation
> 1
Building native extensions. This could take a while...
Successfully installed fxri-0.3.6
Successfully installed fxruby-1.6.11
Installing ri documentation for fxruby-1.6.11...
Installing RDoc documentation for fxruby-1.6.11...
And even more amazingly, all the fifteen thousand of the API docs (builtins and gems) are there! Now the only thing weird was I had to fix the path env in the fxri start script.
Saturday, June 23, 2007
Timing out Child Processes

I finally ran across a short Ruby powerpoint lecture by a CMU grad student to get me on the right track. Basically I wanted to spawn a process that me eventually killed (or disappears magically) after a certain amount of time. The answer is to use Timeout.
Here is my code. Notice I got rid of the fork.
#!/usr/bin/env ruby
require 'time'
require 'timeout'
require 'pp'
def time_cmd(command,timeout)
cmd_output = []
puts "Entering time_cmd at #{Time.now.tv_sec}"
begin
status = Timeout.timeout(timeout) do
p = IO.popen(command) do |f|
puts "pid: #{$$}"
f.each_line do |g|
cmd_output << g
system("ps aux | grep netstat")
end
system("ps aux | grep netstat")
end
end
pp status
rescue Timeout::Error
puts "\n#{command} completed at #{Time.now.tv_sec}"
return cmd_output
end
end
puts "pid: #{$$}"
puts time_cmd("netstat -dwh 1",3)
system("ps aux | grep netstat")
With the following output
franz-g4:~/dev/playin/ruby mdfranz$ ./angrytimeout.rb
pid: 3658
Entering time_cmd at 1182709241
pid: 3658
root 3659 2.2 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 1.3 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.1 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
root 3659 0.0 0.0 27332 368 p2 S+ 1:20PM 0:00.01 netstat -d
netstat -dwh 1 completed at 1182709244
input (Total) output
packets errs bytes packets errs bytes colls drops
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
What I still haven't figured out how to do is get the process id of what is spawned by the popen so that I could Process.kill it if I needed to. I know I have been able to do this but it must have been with the Python subprocess module. which of course is more full featured.
Wednesday, June 6, 2007
Running Ruby on Ancient Solaris Boxes
If you think I hate Ruby, well I hate Solaris even more. It's lack of GNU. Hell, it's lack about about anything useful built-in. It's like *BSD on steroids.
But believe it or not I (sort of) even managed to get Ruby 1.8.6 built and (sort of) running on an old 2.6 and 2.8 box today. It nearly killed the 2.6 box (an Ultra-5) but here's how I did it:
But be warned, all I ran was a simple hello world app and parsed a YAML file, so it will be interesting to see what works and what sends these boxes to a timely death.
But believe it or not I (sort of) even managed to get Ruby 1.8.6 built and (sort of) running on an old 2.6 and 2.8 box today. It nearly killed the 2.6 box (an Ultra-5) but here's how I did it:
- Make sure you at least have gcc, GNU Make, and a version of of GNU Grep in your path somewhere. You probably need more. The latter were things that I know I tripped up on.
- Rip out most of the directories in ext. All I had left in mine were: enumerator, etc, fcnctl, io, pty, racc, socket, socket, stringio, strscan, syck, thread.
- If you need to use YAML, change the first require in yaml.rb to require 'yaml/stringio'
But be warned, all I ran was a simple hello world app and parsed a YAML file, so it will be interesting to see what works and what sends these boxes to a timely death.
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
What gives? See gets documentation for the answer.
The solution is to use $stdin.gets
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
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
On a related note segment7 has some nice blog entries on documentation
So how do you fix this?
Install it with rdoc -R from within the directory that has the .rb
Make sure it is there with ri -l
# rdoc -R oniguruma.rb
oniguruma.rb: mcc..........c....c.....
Generating RI...
Files: 1
Classes: 4
Modules: 1
Methods: 19
Elapsed: 2.334s
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
Wednesday, May 16, 2007
Oniguruma and Named Regexes in Ruby 1.8
Unlike Python and C# (and probably others) Ruby 1.8 does not support named groups. This is available in 1.9 though. Or so I hear.
What are named groups? Basically they allow you to reference the results of the match option with the nice names you embedded in the regex object. Since Blogger hates anything with <>'s you can see an example here . Look near synopsis.
So how do you get this in 1.8.6?
Windows users have it easy, just install the win32 GEM, but OSX (and I assume other UNIX) require a bit of extra steps and it is sort of confusing because there are lots of versions of the Oniguruma C Library (do I really want to download stuff hosted on geocities Japan?) to choose from and there is a way to install it which requires recompiling your Ruby but that only works for 1.8.4 so I didn't bother.
So here is what I did to get it working on OSX (PPC) with 1.8.6
1. Download 4.6.2 of the C Library. Configure with whatever prefix you are using for Ruby (I use /my) and make install
2. Download version 1.10 of the GEM Source Tarball and do the standard ruby extconf.rb dance within ext. However
Change INFLAGS in the Makefile so it can find the oniguruma.h
Mine looks like this because ruby is installed in /my
INCFLAGS = -I. -I. -I/my/include -I/my/lib/ruby/1.8/powerpc-darwin8.9.0 -I.
If you don't do this you will get
If you compile it successfully you'll see the library (oregexp.bundle, whatever that is) put in /my/lib/ruby/site_ruby/1.8/powerpc-darwin8.9.0 (or whatever you path is)
However require 'oniguruma.rb' will still fail until you do this:
But it if you did get it installed you can run the test suite
But of course the example from the README doesn't work (either on OSX or Windows) in typical Ruby fashion.
Simple, easy, and fun!
What are named groups? Basically they allow you to reference the results of the match option with the nice names you embedded in the regex object. Since Blogger hates anything with <>'s you can see an example here . Look near synopsis.
So how do you get this in 1.8.6?
Windows users have it easy, just install the win32 GEM, but OSX (and I assume other UNIX) require a bit of extra steps and it is sort of confusing because there are lots of versions of the Oniguruma C Library (do I really want to download stuff hosted on geocities Japan?) to choose from and there is a way to install it which requires recompiling your Ruby but that only works for 1.8.4 so I didn't bother.
So here is what I did to get it working on OSX (PPC) with 1.8.6
1. Download 4.6.2 of the C Library. Configure with whatever prefix you are using for Ruby (I use /my) and make install
2. Download version 1.10 of the GEM Source Tarball and do the standard ruby extconf.rb dance within ext. However
Change INFLAGS in the Makefile so it can find the oniguruma.h
Mine looks like this because ruby is installed in /my
INCFLAGS = -I. -I. -I/my/include -I/my/lib/ruby/1.8/powerpc-darwin8.9.0 -I.
If you don't do this you will get
gcc -I/my -I. -I. -I/my/lib/ruby/1.8/powerpc-darwin8.9.0 -I. -fno-common -Wall -c oregexp.c
oregexp.c:2:23: error: oniguruma.h: No such file or directory
oregexp.c:9: error: parse error before 'regex_t'
oregexp.c:9: warning: no semicolon at end of struct or union
oregexp.c:10: warning: type defaults to 'int' in declaration of 'ORegexp'
oregexp.c:10: warning: data definition has no type or storage class
oregexp.c:15: error: parse error before '*' token
oregexp.c: In function 'oregexp_free':
oregexp.c:16: warning: implicit declaration of function 'onig_free'
oregexp.c:16: error: 'oregexp' undeclared (first use in this function)
oregexp.c:16: error: (Each undeclared identifier is reported only once
oregexp.c:16: error: for each function it appears in.)
oregexp.c: In function 'oregexp_allocate':
oregexp.c:21: error: 'oregexp' undeclared (first use in this function)
oregexp.c: At top level:
oregexp.c:27: error: parse error before '*' token
oregexp.c:27: warning: return type defaults to 'int'
oregexp.c: In function 'int2encoding':
If you compile it successfully you'll see the library (oregexp.bundle, whatever that is) put in /my/lib/ruby/site_ruby/1.8/powerpc-darwin8.9.0 (or whatever you path is)
However require 'oniguruma.rb' will still fail until you do this:
cp oniguruma.rb /my/lib/ruby/site_ruby/1.8/
But it if you did get it installed you can run the test suite
franz-g4:/tmp root# ruby test_oniguruma.rb
4.6.2
Loaded suite test_oniguruma
Started
..............................................
Finished in 0.046221 seconds.
46 tests, 105 assertions, 0 failures, 0 errors
But of course the example from the README doesn't work (either on OSX or Windows) in typical Ruby fashion.
Simple, easy, and fun!
Subscribe to:
Posts (Atom)
