Ruby feature: Regular Expression replace once

About a year ago, programmer Tony Arcieri posed a question on the Ruby language bug tracker:  Why was Thread.exclusive deprecated?

Among the answers to this question, Shyouhei Urabe’s one of the tersest feature descriptions yet: “we already have such thing, to some extent at least”:

  1. The pair of slashes are Regular Expression delimiters.
  2. A Regular Expression in Ruby allows String interpolation, just like the double-quoted String does. The #{} contains interpolated Ruby code.
  3. The modifier o at the end of the Regular Expression stands for “once”. So, the regex engine would keep track of this replacement, and do it just once.

The Malmo.rb named this expression (//o) the face-palm operator.

Install ruby-2.0.0-p195 using rbenv, ruby-build and pkg-config

Ruby, Rails, news of new versions. Your computer’s Homebrew and ruby-build installation does not work. Something’s borked. It worked on the other people’s Debian machines. Your Homebrew system gave you… output. Some obscure error about openssl?

You search. You whine. You arrive here. Breathe.

Requirements: rbenv, ruby-build, a Ruby 2.0.0-p195 refuses to install on account of missing header files for openssl, since the Apple-bundled version is outdated.


Add this at the end of your shell startup file (such as ~/.bash_profile or ~/.zshrc). Then run the installation again.

export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig:/usr/local/lib/pkgconfig

Learn how!

In order to share more of the fact-finding process, here is some narrative. All credit is due to Stuge. He told me to read, it would work. Thanks! He was right, too. It did work.

First of all, pkg-config is a neat little program. It’s the hero of this narrative. Does all the heavy archiving, noting things down, so you don’t have to.

pkg-config --debug

This lists everything that pkg-config knows about header file locations, libraries, compilation configuration settings about the libs on your system.

Begin by ensuring there is a PKG_CONFIG_PATH on your system, that points to where the currently-symlinked Homebrew packages are. Add this to your shell startup:

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

Well, this is half-way, pkgconfig can now find most of its .pc files.

The specific complaint was about openssl. Let’s go to its folder, using brew:

$ cd `brew --prefix openssl`

Now, are there any pkg-config files around? Use find:

$ find . -name '*.pc'


$ cd lib/pkgconfig
$ pwd

This is the path where our pkg-config descriptions sit. Pre-pend it to its path!

export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig:/usr/local/lib/pkgconfig

Thusly, the desired result:

$ ruby-build 2.0.0-p195 ~/.rbenv/versions/2.0.0-p195
Downloading ruby-2.0.0-p195.tar.gz...
Installing ruby-2.0.0-p195...
Installed ruby-2.0.0-p195 to /Users/olle/.rbenv/versions/2.0.0-p195

The end.

Addendum for web-crawling robots

(The obscure error mentioned at the start included here for searchability.)

rbenv install 2.0.0-p0
Downloading ruby-2.0.0-p0.tar.gz...
Installing ruby-2.0.0-p0...

Inspect or clean up the working tree at /tmp/ruby-build.20130506102349.2564
Results logged to /tmp/ruby-build.20130506102349.2564.log
Last 10 log lines:
installing default gems: /home/shivin/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0 (build_info, cache, doc, gems, specifications)
bigdecimal 1.2.0
io-console 0.4.2
json 1.7.7
minitest 4.3.2
psych 2.0.0
rake 0.9.6
rdoc 4.0.0
The Ruby openssl extension was not compiled. Missing the OpenSSL lib?

Another Ruby conference

The Ruby folks in Texas know how to please a soft-heart like me:

This is a non-profit conference. The organizers are not paid and any profits will be used
for future conferences.

Also, the financial books are open and we will be publishing payables and receivables.
The purpose of this is to help other conference planners and to assure attendees that their money is well spent.

Very impressive, and great. Messrs Jim Freeze and David Bluestein II and Damon Clinkscales – big up from the Oresund Ruby massive.

Also: The format of a “pre-conference conference”, where the attendance fee is a donation to a charity, is a neat pattern that more and more conferences follow.

Powered by ScribeFire.

Speaking at RuPy conference in Poland

Hey! Even more Ruby crap.

They said yes. The RuPy conference (at Adam M University, in Poznan, Poland) wants me to run a RubyGems tutorial.

invited speaker

To make it a pleasant event, I’ve started thinking about how to make Win32 users comfortable. Anyone of my readers using Ruby on Windows?

What I want my co-tutorialists (tutees?) to have: MinGW installed and working. To get that in place, I’ll be trying to work on that here on the blog, writing instructions as I go along.

Are you able to compile Ruby C extensions on your Windows machine? Please make a comment, or send me an email. (olle at the insane domain “olleolleolle daaaaaht dk”.)

Ruby and libxml: Collect your XML::Node::Set

Use collect and map in Ruby says Lucas Carlson. Thanks for pointing out that code arthritis, Lucas.

So, I was working with the libxml-ruby library (fast XML parsing) and found some loops in my code which could be converted. But, XML::Node::Set did not support collect. I mumbled something inaudible, grabbed the Pickaxe book, found the section on Enumerable, read for a bit, then just tried to mix it in:

# Mixes in Enumerable, to enable "collect" and friends
module XML
  class XML::Node::Set
    include Enumerable

I put this at the top of my own library file, and suddenly I could do stuff like:

# photo is a XML::Node::set
photo.find('tags/tag').collect { |tag| tag.child.to_s }
# => ["funky", "rubylicious", "functional"]

This does an XPath expression, and walks through the result, collecting results, and then returns the whole chunk as an Array of tags as strings.

Thanks, Lucas, thanks libxml-ruby, thanks Matz.

*Update*: This mailing list post says that the above is going into 0.4, which seems to be coming along at a brisk pace. Some fantastic news in the shape of an upcoming patch that adds XmlTextReader API support. Good times.

Installing libxml-ruby for libxml2 with Ruby, on OS X

Update 2012: This material was written in 2006. Now, many years later, it still poisons the search engines with its presence. If you want to use Ruby and a package manager on OS X, you should use RVM and Homebrew. I will leave this page here as a warning to you, dear reader. Do not take detailed old technical Ruby recipes as advice.

Just had an exciting package management adventure with Ruby and XML on OS X. I wanted Libxml2 wrapped in a Ruby gem. So, I ran

sudo gem install libxml-ruby

…and watched the software fail to build. Darn OS X not “just being Linux”. After complaining to Christoffer, I was kindly pointed to a MacPort:

sudo port install rb-libxml2

OK, that worked. But I don’t want to use DarwinPorts/MacPorts for my Rubygems, the gems change far too often for that. What I wound up doing was reinstall my gem (sudo gem install libxml-ruby), and then copy the libxml.bundle to its folder:

# cp /usr/local/lib/ruby/gems/1.8/gems/xml/libxml.bundle \

Also, go into the file


and change the required file’s name from xml/libxml_so to xml/libxml.bundle.

This was enough to make the thing work!

Moral: The right way to fix this would be to enhance the Makefile of the libxml-ruby gem to teach it about the OS X environment, but: not me, not today. This little tip is what I can do today. Ciao.

irb with wirble

Using irb with your Ruby? Yeah, I thought so.

If you want syntax coloring, ri inside irb and a shorter prompt, get Wirble:

sudo gem install wirble

And add an .irbrc file to your home directory, following Wirble’s advice on what to put there.

Oh, and when you have one of those, you could add Ben Bleything’s command history to your irb, while you’re at it. (Minor update: Ben’s called Ben, not Adam.)

Makes for a more communicative irb experience.


Go snippet away, Rubyboy

Just started my morning with fixing a personal annoyance with the current Rails distribution I am using with Ruby 1.8.5: warning messages about deprecated syntax. All I did was heed the advice in the warnings and edit all the when and else control structures that were using colons to using semi-colons. After that, my Rails commands run silently.

High on simple fixes, I went to RubyForge, and beheld the fancy-looking web site, and went to look at Code Snippets. Whoa, lots of goodies there.

Selenium on Rails: extra-special testing tool

When Obie F. was in town, he ended his talk by showing a final, extra-special productivity-boosting developer tool: Selenium on Rails. As it happens, it is our own Jonas “Danger Bay” Bengtsson who created that Rails plugin, on top of the ingenious Selenium testing software.

Jonas’ own screencast is a little dated, but it shows off perfectly what Selenium on Rails does. Take a look at it. Again, if you already did: now it comes with Obie’s seal of approval, should you need such a thing.