Archive for the ‘Ruby’ Category.
October 27, 2006, 3:19 pm
I’m been doing some work in Ruby to build a parser framework and I wanted to have pluggable parsers from the command line. I figured this would be simple because in Java it actually is pretty simple, as long as the pluggable parser is in the classpath. Well, with Ruby, this isn’t quite the case. Even if a class is in the load path, it still isn’t visible until it has been loaded either via the load method or the require method on Kernel. This is annoying and something I wasn’t used to. But a quick hackery fixes this nicely:
# Load all the local rb files
Dir.glob("*.rb").each do |d|
load(d) unless d == $0
end
This loads every *.rb file in the current directory into the current Ruby process. Then you can do some String reflection magic to load a class:
parser = eval(ARGV[0] + "Parser.new(ARGV[0].downcase())")
This allows me to call my scriptage and pass in the prefix of the class name like this:
ruby scriptage.rb MySpecial
This will then instantiate an instance of MySpecialParser. I could have iterated over all the constants in the entire Ruby process and found the symbol I needed and then called new on that, but that sucks. Eval is much cleaner in this case.
In fact, in Java loading a class by name is simple:
Class.forName("com.inversoft." + prefix + "Parser");
In Ruby this doesn’t work so well. You have to first load all the .rb files like I did and then use eval. They really should add something to Class or ObjectSpace or Kernel to do all this for you. In fact, I think in 2.0 load_path should be able to load all files in the load path via reflection without any requires or load crapola.
August 6, 2006, 1:56 pm
The default timeout for FCGID is pretty low I think (maybe 5-10 seconds). This was causing me SO much grief that I was just about to lose it. I found these posts out there that helped a lot:
http://wiki.rubyonrails.org/rails/pages/Debian+mod_fastcgi+Notes
http://weblog.rubyonrails.com/2005/01/03/watch-for-huge-requests-on-default-fcgi
Of course Ubuntu uses different config locations and such for FCGID, but the configuration names are the same.
I set my timeouts to 2 minutes just to be safe. Besides for an admin application, I could wait for a long time without thinking things were amiss. Anyways, help that helps prevent some folks from losing their minds.
August 6, 2006, 12:05 pm
Oh my god! This one pass Ruby parser is really annoying. Here’s todays example that doesn’t compile:
DICTIONARY = create_dictionary
def MyClass.create_dictionary
...
end
Yep, you got it, this blows up with a nice error message stating that the variable or method named create_dictionary couldn’t be found. Move the constant definition below, everything is fine. Lame.
August 1, 2006, 8:53 am
I’m been doing a lot of work with RoR plugins trying to make things generic. The RoR plugin mechanism is just plain horrible. Plugins do not work like most ruby classes because they are loaded in a slightly convoluted way and most errors end up getting swallowed by RoR. Of course this can be fixed, but it is really annoying and makes programming even more stone-age than it should be. You end up with errors like this:
/usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123: in `const_missing': uninitialized constant AjaxValidation (NameError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:in `const_missing'
from script/../config/../vendor/plugins/ajax_validation/init.rb:3: in `load_plugin'
...
The issue here is NOT that AjaxValidation isn’t defined. Instead, it is that in the definition of AjaxValidation an error has occurred and this caused the load of AjaxValidation to completely fail AND there is absolutely no logging of the error that caused AjaxValidation to fail. So, you resort to writing to stdout each line of code to figure out where the problem is. That sucks.
Another issue I have with plugins is that I cannot define standard action methods in a plugin. There should be a mechanism for doing this. The issue seems to be that how RoR is handling action method invocation is not standard reflection because this would mean that all methods on the object could be actions. Instead it only allows the methods defined in a controller to be called. This makes sense, but makes life hard on plugin developers.
July 28, 2006, 3:16 pm
I wanted a ruby version of the Java Locale object and couldn’t find one, so i created a project for it. Here’s the address:
http://rubyforge.org/projects/locale/
It is pretty minimal, but it does contain all the current ISO country and language codes as well as a simple class for accessing them. It is also in a rails compliant plugin layout so it can just be dropped into the rails vendor/plugin directory.
The unit tests are failing, but it is just a numbering issue, which I’ll fix this afternoon or next week.
Enjoy.