Regex Replacer
Here is a little Ruby library that may be useful to you. It is a regular expression string replacing tool. It’s similar to what you can do with String#gsub in Ruby, but with a few more features.
I needed this library to get Textmate snippets working in Redcar, and therefore it uses a slightly different syntax to gsub (”$1″ instead of “\1″). Here are some examples:
rr = RegexReplace.new(/nuclear (\w+)/,
"solar $1")
rr.rep("nuclear nutcracker")
=> "solar nutcracker"
So far so obvious. That’s identical to Ruby gsub but with “\1″ changed to “$1″. Here’s something more useful:
rr = RegexReplace.new(/nuclear (\w+)/,
"solar \\u$1")
rr.rep("nuclear nutcracker")
=> "solar Nutcracker"
Whee! Letters following “\u” are upcased. What else can it do?
Upcase stretches with \U…\E
rr = RegexReplace.new(/nuclear (\w+)/,
"solar \\U$1\\E")
rr.rep("nuclear nutcracker")
=> "solar NUTCRACKER"
You can downcase in exactly the same way with “\l” and “\L..\E”.
Conditional replacement with (?1:then:else)
rr = RegexReplace.new(/nuclear( \w+)?/,
"solar(?1:$1: thingy)")
rr.rep("nuclear nutcracker")
=> "solar nutcracker"
rr.rep("nuclear")
=> "solar thingy"
You can also combine the operators in this fashion “\l\U…\E” to get (for instance) “fOO” from “foo”.
If you find it useful please let me know.
Ruby WebKit 2
The Ruby-WebKitGtk bindings are usable. Clone them:
git clone git://github.com/danlucraft/rbwebkitgtk.git
or download them. I’m using the WebKit nightly r31823, YMMV. Build instructions for WebKitGtk are here.

To do:
- Finish the methods on WebView.
WebHistoryListWebHistoryItem
Ruby WebKit
Update: These bindings are now pretty usable. See here.
This morning I started working on some Ruby bindings for WebKit on GTK. I’ve only been working on them for an hour or so but they can already do this:
require 'src/lib/webkit'
win = Gtk::Window.new
wv = Gtk::WebKit::WebView.new
win.add(wv)
win.show_all
wv.open(’http://www.google.com’)
Gtk.main
to get this:
The Ruby-Gnome2 team have done a fantastic job of making it easy to write Ruby to GTK bindings. I hope to make good progress on these tomorrow.
New version of Rak
I released a new version of Rak today with some bug fixes and enhancements.
New features:
- Colouring works on Windows if win32console gem is installed, fails gracefully if not.
- Added .rake, .erb and .haml to Ruby filetypes.
- Added match only at end-of/start-of line options
Thanks to Edvard Majakari, Kevin Ballard, Esad Hajdarevic, Bela Babik, Genki Takiuchi and Rob Knight for suggestions and fixes.

Using Meta-Programming for Performance in Ruby
Normally we use meta-programming in Ruby for our own convenience as developers, and we swallow the speed hit it gives us as a reasonable trade-off. The way Rak is implemented turns this on its head.
Rak compiles its line matching code on the fly, to speed up searching. Its not as horrendous as it sounds. Here’s a very simplified version of the line matching code without the optimization:
def line_match(filename, regex, line)
if options[:invert_match]
unless line =~ regex
if options[:print_filename]
puts filename
end
puts line
end
else
if line =~ regex
if options[:print_filename]
puts filename
end
puts line
end
end
end
We notice that the options hash never changes once the searching has begun. But we are doing an awful lot of work checking the values in the hash for every single line we are matching against. So we replace it with this:
def compile_line_match(filename, regex, line)
code = []
code << %{def line_match(filename, regex, line) }
if options[:invert_match]
code << %{ unless line =~ regex }
if options[:print_filename]
code << %{ puts filename }
end
code << %{ puts line }
code << %{ end }
else
code << %{ if line =~ regex }
if options[:print_filename]
code << %{ puts filename }
end
code << %{ puts line }
code << %{ end }
end
code << %{end }
module_eval code.join("\\n")
end
I’ve preserved the indenting so you can see the logic of the method. Here all the option tests have been factored out and will only be done once, at startup. Assuming we are not inverting the match and are printing filenames, the final compiled method looks like this:
def line_match(filename, regex, line)
if line =~ regex
puts filename
puts line
end
end
Much smaller. This method is 140 lines long in the full version, so its a much more impressive win there. All told this sped up Rak by about two times when I tested it searching on my Phd repository (3500 files).
There’s more work to do optimizing Rak. It’s still not as fast as Ack, so you might want to use that unless you are a Ruby fanboy like me :). But I have some more ideas….
Update 20/04/08: thankyou to redditer tomel for pointing out a WTF in the above code :).
Rak, a grep replacement in pure-Ruby
Rak is a tool for searching directories for files matching a regexp, like a more convenient grep. It gives you pretty highlighted output, and uses the Ruby regexp syntax. To install type gem install rak.
Rak is implemented in pure Ruby, so it should work on all platforms. Windows folk will probably find they need to use the –nocolour option (I will fix this next time I boot into Windows for any reason).
NB. Rak is an almost perfect clone of the Perl tool Ack by Andy Lester. He should get all the credit for the idea.
Rubinius IRC Logs
Missing an issue of my favourite magazines - PC Plus, Edge and The Economist, causes me almost physical pain. I can’t stand the gap.
So I haven’t taken to IRC, just because I know I’m always missing something. This week I’ve been looking at the Rubinius project, which eschews a mailing list for IRC, and it seemed like a log of the discussions would be a good idea. So here’s a simple one in Ruby: the Rubinius IRC logs.
Comparing Erlang and Prolog
In the back of my mind, I have for a little while known two things:
- Prolog is no language for a man to use.
- I should probably check out this Erlang thing.
So today I have a look at this getting-started-with-erlang tutorial, and lo! Perhaps my Prolog skills are not so useless after all.
% Erlang
-module(tut4).
-export([list_length/1]).
% simple function to return list length
list_length([]) ->
0;
list_length([_F|R]) ->
1 + list_length(R).
% Prolog
:- module(tut4).
:- export list_length/2.
% simple predicate to return list length
list_length([], 0).
list_length([_F|R], L):-
L is 1 + list_length(R).
Similarities so far:
- Modules - one per file.
- Module/export directives very similar.
- Commas go between statements, full stops end code blocks.
- Comments use the same symbol.
- Multiple function clauses with argument matching.
- Lists work in same manner: [head|tail].
- Variables begin with capitals.
- Atoms begin with lowercase, (atoms are the same as Ruby’s symbols).
- Singleton variables generate compiler warnings, unless they are prefixed with an underscore (_F above)
- Both scope modules with a colon.
- To quit the shell you call ‘halt’.
Differences:
- Erlang functions have return values, rather than the Prolog concept of success and failure, and passing return values out through arguments.
Fix for Ruby Gnome GtkMozEmbed
After upgrading to Feisty, any attempt to use the Ruby-Gnome2 Gtkmozembed bindings fails with:
$ ruby ruby-gecko.rb
ruby-gecko.rb:203: [BUG] Segmentation fault
ruby 1.8.5 (2006-08-25) [i486-linux]
Aborted (core dumped)
After an hour of searching online, I discover that (a) the problem isn’t limited to Ruby, and (b) the fix is quite simple:
$ export LD_LIBRARY_PATH=/usr/lib/firefox
It’s things like this that make you crazy.
Null Objects
Tom Locke and I have similar proclivities. He says:
class Object
def _?()
self
end
end
class NilClass
def _?()
SafeNil.new
end
end
class SafeNil
def method_missing(*args, &b)
nil.send(*args, &b) rescue nil
end
end
I say:
class Object
def null
n = nil
def n.method_missing(*args)
null
end
n
end
end
The difference:
string_or_nil._?.length # Tom's
(string_or_nil||null).length # mine
I like being able to go more than one method deep,
(string_or_nil||null).length * 4
But that might not be to your taste.