Upgrading RubyGems and building binary Windows distributions
July 28th, 2008
I have been using RubyGems 0.9.4 for quite a while, putting off upgrading to 1.* due to the problems it brings with building Gecode/R. The problem is that Gecode/R has a binary distribution with pre-compiled binaries for Windows. The binary distribution has to be marked with its platform to keep it separate from the source distributions. Before RubyGems 1.* this was done by setting the gemspec’s platform attribute as follows
spec.platform = Gem::Platform::WIN32
This however stopped working in 1.*, returning the following error message.
WIN32 has been removed, use CURRENT instead
All Gem::Platform:: constants other than RUBY and CURRENT were removed.
The RubyGems user documentation website hasn’t been updated since 0.8.7, so it isn’t easy to find information on why this change was made or how one should now build binary packages for platforms other than the host platform. Looking at the RubyGems source code however reveals that there is still some legacy support; one just has to avoid using the removed constants. The solution is to set the platform in the gemspec using
spec.platform = 'mswin32'
and things will be back to normal (until the legacy support is removed).
Command line Ruby
November 7th, 2007
This is a handy template to start from when you want to use Ruby on the command line instead of shell scripting. The template just lists all files in the current directory, but is easy to extend.
ruby -e "require 'rubygems'; require 'rake'; FileList['*'].each{ |x| puts %x{ls -l \"#{x}\"} }"
Matlab matrix to LaTeX
September 30th, 2007
Here’s a handy Ruby one-liner for converting Matlab output with matrices to a form that can be directly pasted into a LaTeX array.
ruby -ne 'puts($_.strip.split(/\s+/).join(" & ") + " \\\\")'
An example:
matlab -r "[1 2 3; 3 5 7], exit" -nosplash -nodisplay | ruby -ne 'puts($_.strip.split(/\s+/).join(" & ") + " \\\\")'
outputs
\\ To & get & started, & type & one & of & these: & helpwin, & helpdesk, & or & demo. \\ For & product & information, & visit & www.mathworks.com. \\ \\ \\ A & = \\ \\ 1 & 2 & 3 \\ 3 & 5 & 7 \\ \\ \\
where the matrix body is ready to be copied directly into a LaTeX array.
A = \left(
\begin{array}{ccc}
1 & 2 & 3 \\
3 & 5 & 7 \\
\end{array}
\right)
GDB + Ruby
July 17th, 2007
An error report shows a nice synopsis of how to use GDB with Ruby.
Google sitemaps in webgen
June 27th, 2007
I recently started using webgen for the Gecode/R website. I have just found a plugin which generates google sitemaps for it.
Code coverage in Ruby
June 6th, 2007
rcov seems like a nice tool for measuring code coverage. It should be tested during the next project.
Creating fresh daemons
May 22nd, 2007
If you need to create daemons in Ruby then I would recommend having a look at the Daemons gem. One problem with using Daemon.call is that everything that has been loaded by the parent process will still be loaded by the new daemon. This can create some annoying problems when the daemon isn’t really alone in it’s process.
I had the above problem recently when writing tests for a daemon. To write the test I spawned several daemons which were then tested. I tried removing and reloading classes, but I were using active records, which interferes with that, so I had to come up with something else. Originally I didn’t want to spawn a new interpreter through e.g. system because I didn’t want to spread the testing codes into several files (one which the new interpreter would be given as argument). Luckily the -e option exists so what I did was to basically spawn a daemon with Daemons.call and then write the script in the daemon’s block and then replace the daemon process with a new interpreter using exec. This is how it turned out.
def setup
# ...
@daemon1 = Daemons.call(:multiple => true, :backtrace => true) do
script =<<-"end_script"
# Set up the daemon along with any test-specific configuration.
end_script
exec "ruby -e '#{script}'"
end
# ...
end
I went through quite a bit of approaches that didn’t work before the above. So if you need to spawn a fresh daemon with its own unused interpreter state then please save yourself some trouble and just use the above.
Unit test hiarchies in Ruby
May 10th, 2007
Some unit tests can become very similar, up to the point where one might want to put some of the support methods into a module that’s mixed into each test. Some times it might also be a good idea to even create a base class that other unit tests can inherit from. Lets say we do something like the following.
class TypeATest < Test::Unit::TestCase
def setup
# Set up some environment.
end
def teardown
# Tear it down again.
end
end
class MyTest < TypeATest
def test_something
#...
end
end
The above will however not work. It will give the error “No tests were specified.” since TypeATest did not specify any tests. That can be worked around by e.g. adding a dummy test such as the following.
def test_truth assert true end
That will remove the error, but instead we get another problem: the test statistics will count the dummy method. So if we have one test in TypeATest then it will count as two tests. The same goes for any test that loads the file containing TypeATest.
Luckily there’s a cleaner way to solve it. The error is given by Test::Unit::TestCase#default_test which has that as the only purpose, so instead of using a dummy test we can overwrite the default_test method to do nothing. The statistics are no longer skewed, the only downside is that one no longer gets an error if MyTest doesn’t contain a test. The example above would become the following.
class TypeATest < Test::Unit::TestCase
def setup
# Set up some environment.
end
def teardown
# Tear it down again.
end
def default_test; end
end
class MyTest < TypeATest
def test_something
#...
end
end
return in the middle of a block
May 4th, 2007
I have been trying to find a way to abandon a block and have it evaluate to a specified value. E.g. replace “return” with something else in the following so that it prints 2. I haven’t been able to find anything so far though.
def foo(&block)
return yield + 1
end
def bar
return foo{ return 1 }
end
p bar
When I first started with ruby i misunderstood return in blocks and assumed that block were just anonymous methods and that return would return control to the caller of the block. After some painful debugging I found out that it wasn’t the case and now find myself cluttering blocks with control structures instead. From what I can find it doesn’t appear to be possible, I’m not sure why.
A good read about blocks and procs
April 22nd, 2007
I would like to recommend an article about blocks and procs in Ruby. It straightened out many of my questions, especially about the differences, and is probably a good read for anyone who wants to have a good grip on Ruby.
Something to keep an eye on
April 14th, 2007
Converting between bytes and hex
April 5th, 2007
Converting from hex to bytes (must be an even number of hex digits):
> '0xaa14'.sub('0x', '').scan(/.{2}/).map{ |x| x.hex.chr }.join
"\252\024"
Converting from bytes to hex:
> "\252\024".unpack('H*').join
"aa14"
I suspect that there’s an easier way to convert from hex to bytes that I have missed.
Converting lists to LaTeX tables
April 1st, 2007
I recently had to typeset a couple of lists of newline-delimiters to LaTeX. Rather than to do the mind-numbing job by hand, I wrote the following script. It takes one argument: the number of columns one wants in the table to have. It then expects the list from standard input, the first row is assumed to be the table captions.
An example:
> to_table 3 Name Has foo Has bar Program1 Yes No Program2 No Yes ^D
Results in
\begin{tabular}{ccc}
Name & Has foo & Has bar\\
\hline
Program1 & Yes & No\\
Program2 & No & Yes
\end{tabular}
Rails tricks
March 3rd, 2007
There’s a list of useful rails tricks. Some of them might prove handy at some point.
Ruby reference
January 24th, 2007