Archive for the ‘ruby’ Category

JRuby, Can’t Find File in Classpath (You Needed a Slash, in

Monday, January 10th, 2011

Update: this is no longer valid as of the release of 1.6.0.

I was debugging this Dubious problem where a class I was sure was on the class path couldn’t be found(link). First, I tried compiling on my local checkout, and it worked oddly. Then I realized I was dumb, because the class was being picked up from the finished jar. So, I clobbered my build artifacts and then my local copy didn’t compile either.

I did some digging and found out why.

In the end it was because of how JRuby implements $CLASSPATH–the way you can modify the classpath from within JRuby. JRuby uses a subclass of URLClassLoader to implement it which gives it some possibly unexpected behavior. In particular, URLClassLoader treats anything that does not end in a slash as a jar. That’s why the class files in my build directory were not being picked up properly.

Lessons Learned:

Combat “It Works on My Machine™”. Clobber local generated files every now and again, and checkout a fresh tree from time to time to reduce the chance something local is weird.

Look for and ask for help when you don’t know something. First time I tried to figure out the problem, I just tinkered and compiled and tinkered and compiled. A little research would have saved some time.

TL;DR

If JRuby can’t find your classes and you think they should have been added to $CLASSPATH, make sure you have trailing slashes on your directory names.

Update:

This has actually been fixed, but not released yet: https://github.com/jruby/jruby/commit/8740a6b3ea946a1442dd7fa833aed3c30d82e23f

RubyConfX: Awesomesauce!? Hells Yes!

Thursday, November 18th, 2010

RubyConf X in NOLA last week was epic.

Epic.

Seriously though, I had a rocking time. I gave part of a talk, I talked to an amazing amount of awesomely smart people and just generally enjoyed my ass off.

It was the first RubyConf I’d been to, and I think the biggest conference I’ve gone to yet as a developer(ALA doesn’t count). I liked being in New Orleans again, surrounded by Jazz and a culture that contains people who think it is perfectly reasonable to drive around playing a trombone out of a car window. It’d be awesome if RubyConf was there next year.

My talk went pretty well I think, though I wish I’d taken more time to practice. It didn’t help that I hadn’t met my co-speaker, Bob Aman before the conference. But, it worked out pretty well, I think (speakerrate may or may not say otherwise). And we got some good feedback on twitter.

New Orleans, as I well know, is a great place to experience. I mean, look at this Po’Boy:

A Large Dressed Roast Beef Po'Boy

Running in the  RubyConf Xk(I ran the 5k) was fun. I totally expected to be unable to jog the whole way, but I did. And, I upped my pace on the second lap coming in at a respectable 29:41. Not bad for someone who has not gotten enough exercise in recent months. Oh, and @tenderlove ran too, in spandex no less:

Heroic Race Finishers

I definitely need to up my silliness for the next Ruby conference I go to. I feel out classed.

All in all, I had a freaking blast.

RubyConf X and Associated Activities

Wednesday, November 10th, 2010

Tomorrow RubyConf begins. And, I will be there. I’m speaking, though not listed in the schedule yet(I’m filling in for John Woodell again). I’ll be talking about Dubious, a web framework written in Mirah, along with Bob Aman, who will be talking about AppEngine and Google’s new discoverable APIs.

I’ll also be doing some ruby processing + DRb stuff like we had at mountain.rb. See video:

I think this is the biggest conf I’ve been to so far, and I’m really looking forward to it.

It should be awesome and a lot of fun.

Mountain.rb

Wednesday, October 20th, 2010

Mountain.rb At The Boulder Theater

My first local conference, right here in Boulder. I had a lot of fun. Thanks Marty for a great conf.

Why Not YAML?

Sunday, October 17th, 2010

I enjoyed Joe O’Brien’s recent post Why Yml?. He asked why we use external config files when we have an dynamic language like Ruby. Yeah, Ruby is more expressive and it would be simpler to use only one language, but to me it makes sense to use YAML for config data. And I think YAML is used a lot because it can represent config data more clearly.

So, why would you store data outside of code when Ruby is so
freaking expressive?

  1. Interoperability — When you have applications in different languages that use the same configuration, then having a separate file makes a lot of sense. For example, when writing a JRuby on Rails app that  has bits of Java code (i.e. servlets w/ JDBC) that share the DB, you want to centralize your DB config to avoid having to do something nasty like generating properties files from database.yml.
  2. Security — Keeping config details, like DB login information out of the source is a good idea. It’s more secure because you often have a lot of sensitive information in your config files. The less places that information appears, the better. They should, ideally, not be checked into source control. For some reason, I feel more uncomfortable with the idea of not checking in source files than I do yaml files.
  3. Convention — Rail’s database.yml has a very stable structure. Keeping it as a YAML file encourages developers to not muck about with it. Why do this? There are times when having less expressivity is better because it’s harder to shoot yourself in the foot that way. It makes adding unnecessary behaviors more awkward. For database connections, this makes a lot of sense because they should not usually have a lot of logic around configuring them.

I generally agree with Joe on the ugly use of ERB in yml files, at least for things like data store configuration. When I see that or find myself doing it, I have a little a sense of “you’re doing it wrong!” Code smell, indeed.

I also think that the YAML vs XML vs JSON discussion is important here, because YAML as a language has some interesting properties that make it well suited to config type situations. In particular, I’m thinking of the ability to name chunks of the document and substitute them in other parts of the document in an override-able manner.

For example, in YAML, a set of configurations where one inherits from another might look like

development: &default
  adapter: foo_store
  port: 1337
  database: base_development
production:
  <<: *default
  database: base_production

The equivalent in Ruby could be represented something like

#using 1.9 syntax, so it looks more yamlish
config_hash = {
  development: {
    adapter: 'foo_store',
    port: 1337,
    database: 'base_development'}
}
config_hash['production'] = config_hash['development'].merge(
                                                database: 'base_production'
)

Which I don’t think is quite as easy to scan.

I guess my point is that while Ruby is very expressive and powerful, sometimes a more constrained language can better express config information in a declarative manner.

Speaking at Ruby|Web Conference on Friday

Wednesday, September 8th, 2010

On Friday morning I am speaking at the Ruby|Web Conference at Snowbird in Utah. I’ll be giving a talk entitled “Extreme Performance with Mirah and Dubious“. I’ll be talking about a project I’ve been helping out with called Dubious, which is a web framework written in Mirah.

Check out the source:

http://www.mirah.org/

http://github.com/mirah/dubious

Mountain West Ruby Conf Rocked This Year

Monday, March 22nd, 2010

Mountain West Ruby Conf this year was awesome. I totally had a blast.

It was the third time I’ve gone to it. It was my first conference and still is my favorite. I got to meet Matz, I gave a lightning talk, I met all sorts of interesting people and I learned a bunch of new things. To quote Matz, MountainWest RubyConfはすばらしかった。

Matz & me

Things to revisit

  • Rack(1.1.0 in particular)
  • Chef(looks better than the last time I poked at it)

New things(to me)

  • RVM(rvm looks pretty handy, especially for cross vm library development)
  • Hubris(this might be a neat way to get into Haskell development)

My lightning talk

Usdo. Misspellings turn into gem-fu practice

Saturday, November 14th, 2009

Indian Paintbrush
A few weeks ago, during a period of frustration, I found myself repeatedly mistyping ‘sudo.’ So, in a fit of silliness I wrote a short script to insult me when I did it, and put it in the path.

Later, I packaged it as a gem, because a) I had never built a gem with an executable and b) gems are a great way to share things.

The end result?

http://github.com/baroquebobcat/usdo

It isn’t particularly smart, but I happen to find it funny. It also showed to me how far gem packaging has come. Gemcutter and Jeweler make building and distributing ruby gems freakishly easy. This is especially awesome in light of the recent announcement that gemcutter is going to be the default gems host(though at rubygems.org)

How I put it together

I ran jeweler to create the default directory structure and added a bin dir to it, for the files I wanted to end up in the path.

$ jeweler usdo
cd usdo
mkdir bin

I put my gist from before in the bin directory, and set up git.

Set up my gem info in my Rakefile

#...
Jeweler::Tasks.new do |gem|
  gem.name = "usdo"
  gem.summary = %Q{adds usdo command to ridicule mispellings of sudo}
  gem.description = %Q{...}
  gem.email = "ndh@baroquebobcat.com"
  gem.homepage = "http://github.com/baroquebobcat/usdo"
  gem.authors = ["Nick Howard"]
end

then did the jeweler gem initialization dance

rake version:write
rake gemspec
rake install

Testing it out:

$ usdo -l
----USDO----
    You mispelled sudo
    You can't do anything, you can't even spell sudo
    are you really sure you want to try running
    'sudo -l'????

Now that was working, to send it up for distribution.

Jeweler is awesome and now has gemcutter support. I followed the jeweler README‘s directions on uploading to gemcutter. Which makes pushing your gem as simple as

$ rake gemcutter:release

Awesome.

Check out my code if you want.

Scrubyt On A Server Workaround

Wednesday, November 4th, 2009

So you got some awesome scraper script and you want to deploy it to your server but you don’t want to have to install firewatir. Or, you don’t want to pull in the firewatir gem because you are not planning on using it. Whatever.

Problem: scrubyt
requires the firewatir gem

Hacky Solution: comment out the require

But where you ask?

/lib/scrubyt/core/navigation/agents/firewatir.rb line 1

This works on the current rubyforge version(0.4.1).
Recommended: Or, you could just use the version on github that is fixed(see commit)

grab the files via git or archive from github.com/scrubber/scrubyt

then

cd scrubyt-somehash
rake gem
gem install pkg/scrubyt-0.4.26.gem

Not as nice as using jeweler where you can just `rake install` it, but pretty nifty none the less.

OpenID Provider in Sinatra

Thursday, September 17th, 2009

As part of a personal project, I have been working on an OpenID Provider written in Sinatra.

The idea is to make it ridiculously easy to set up an openid provider within a rack middleware stack.

It’s just a single sinatra app without any tests using code I cribbed from the example rails implementation in ruby-openid. But, I intend on rewriting it once I get a better feel for how it should go.

The problem I face is a matter of interface, specifically application programming interface. I want it to be simple to use. But that is alittle complicated underneath.

Today I read The Little Manual of API Design(pdf), which made me think about my approach to building things in, I think, a good way. It talks about the goals of building a good API and boils it down to five things.

  • Easy to learn and memorize
  • Leads to readable code
  • Hard to misuse
  • Easy to extend
  • Complete

For my app, complete is pretty easy. I’ve boiled the interface down to two main things:

  • OpenID Store
  • User/Identity

First, for an openid app, you need an openid store to put all the associations, nonces etc.

Then, the meaty bit, the users and their identities. The stuff we are serving up. My current thought is to have one of the app’s parameters be a lambda or Proc that returns the current user, or nil when called with the request.env. So that using it would look somewhat like this.
It’s kind of ugly, but I like it better than some of my other options. One other way I could get the user to the app would be to use rack request hash. I could have users put the user object in the request env before the OpenID provider gets it and tell the provider where to look. But the problem with that is that I would need to add another middleware layer that added the user to the environment. That would look something like this:

Maybe that would be better.

Ultimately I just need to pick something and run with it.

The thing I wonder about though, is what is the most idomatic way of doing this. Using the most rubyish, most Rack like interface would make it easier to learn and memorize.