tags:

views:

229

answers:

3

I'd like to write a Ruby snippet that gets run when my Gem is first installed via [sudo ]gem install mygem. Can it be done?

A: 

You can try to do this using call of OS commands. I'll quote eample from irb but you can do same in your scripts too.

irb(main):001:0> system 'gem list | grep rails'
rails (2.1.1, 2.1.0)
=> true
irb(main):002:0> system 'gem list | grep railssssss'
=> false

You can use result of this command as the condition of your snippet execution.

IDBD
+1  A: 

It doesn't look like it's really supported. I found a "post_install_message" attribute that you should be able to set in the gem spec, but that won't execute code.

You may be able to do it by packaging your on-install code as an extension in your gem (as if it were a native extension), and providing a Rakefile to "build" the extension (i.e. call your code).

Matt Burke
+1  A: 

See my blog post: http://blog.costan.us/2008/11/post-install-post-update-scripts-for.html

Great hacking, Victor!
James A. Rosen