This article is obsolete since the bug was corrected in core a long time ago.

Seems there’s a problem with the Rails generator. I was receiving an odd (pun intended) error:


1 $ ruby script\generate migration -tc AddGamesAndCategories1
2 odd number of arguments for Hash
3 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:130:in `[]’
4 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:130:in `add_general_options!’
5 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:130:in `call’
6 C:/ruby/lib/ruby/1.8/optparse.rb:1308:in `order!’
7 C:/ruby/lib/ruby/1.8/optparse.rb:1266:in `catch’
8 C:/ruby/lib/ruby/1.8/optparse.rb:1266:in `order!’
9 C:/ruby/lib/ruby/1.8/optparse.rb:1346:in `permute!’
10 C:/ruby/lib/ruby/1.8/optparse.rb:1373:in `parse!’
11 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:89:in `parse!’
12 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:85:in `initialize’
13 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:85:in `new’
14 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../options.rb:85:in `parse!’
15 ./script/../config/../vendor/rails/railties/lib/rails_generator/scripts/../scripts.rb:19:in `run’
16 ./script/../config/../vendor/rails/railties/lib/commands/generate.rb:6
17 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require__’
18 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in `require’
19 script/generate:3

After a bit of debugging, it turned out that there’s a small problem with the Rails::Generator::Options::ClassMethods#add_general_options! method.



1 opt.on(-c, —svn,
2 Modify files with subversion. (Note: svn must be in path)) {
3 options[:svn] =
4 Hash`svn status`.collect { |e|
5 e.chop.split.reverse unless
6 e.chop.split.size != 2


1 opt.on(-c, —svn,
2 Modify files with subversion. (Note: svn must be in path)) {
3 options[:svn] =
4 Hash[
`svn status`.collect { |e|
5 e.chop.split.reverse unless
6 e.chop.split.size != 2 }.flatten] }

(reformatted for visibility)

This line will return nil, unless the line splits in two (A app/models/test.rb). The problem is that if the number of lines that don’t split in two is odd, then the flattened array will contain an odd number of items, which will cause Hash.new to fail.

The solution ? compact the array before flattening. I already submitted ticket #2814 with a patch.

In the meantime, I’m running a patched copy of Rails.

UPDATE: revision 2972 applied a modified version of my original patch.

Search

Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend

Links

Projects I work on

Projects I worked on