The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
LibInject 0.1.0: Small Pieces Smooshed Together

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Francis Hwang

Posts: 130
Nickname: francis
Registered: Jul, 2004

Francis Hwang is the Director of Technology at Rhizome.org.
LibInject 0.1.0: Small Pieces Smooshed Together Posted: Aug 29, 2005 3:04 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Francis Hwang.
Original Post: LibInject 0.1.0: Small Pieces Smooshed Together
Feed Title: Francis Hwang's site: ruby
Feed URL: http://fhwang.net/syndicate/ruby.atom
Feed Description: Author & artist Francis Hwang's personal site.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Francis Hwang
Latest Posts From Francis Hwang's site: ruby

Advertisement

I’ve just released the first version of LibInject, which is a developer tool for injecting external dependencies into a Ruby file. More specifically, it finds require statements that point to files that aren’t in the standard library, expands those files, and drops the raw text into the original file.

Why would somebody do this instead of just using a plain old require statement? One reason might be because you want to write a one-file script that can also have external dependencies. I wrote LibInject for use with FeedBlender, which is intended as a one-file script aimed at Ruby newbies who might not even know how to install from .tar.gz files or from RubyGems. With LibInject, I can have external dependencies in FeedBlender, use a Rake task to inject those libraries into feedblender.rb, and then distribute the end result.

The project page for LibInject is http://rubyforge.org/projects/libinject/.

For example, let’s say you’ve three files, a.rb, b.rb, and c.rb. a.rb requires b.rb and c.rb, and b.rb requires c.rb:

a.rb

require 'b'
require 'c'
class A; end

b.rb

require 'c'
class B; end

c.rb

class C; end

If you wanted to distribute a version of a.rb without any external dependencies, this is how you’d run LibInject:

require 'libinject'
puts LibInject.lib_inject( File.open( 'a.rb' ) )
# We could also pass LibInject.lib_inject a String, like so:
# contents = File.open( 'a.rb' ) do |f| f.gets( nil ); end
# puts LibInject.lib_inject( contents )

And this is what you’ll get:

# v---------v---------v---------v---------v---------v---------v---------v
# LibInject: begin 'b' library injection
# v---------v---------v---------v---------v---------v---------v---------v
# v---------v---------v---------v---------v---------v---------v---------v
# LibInject: begin 'c' library injection
# v---------v---------v---------v---------v---------v---------v---------v
class C; end
# ^---------^---------^---------^---------^---------^---------^---------^
# LibInject: end 'c' library injection
# ^---------^---------^---------^---------^---------^---------^---------^
class B; end
# ^---------^---------^---------^---------^---------^---------^---------^
# LibInject: end 'b' library injection
# ^---------^---------^---------^---------^---------^---------^---------^
class A; end

The code looks sort of messy, but now it’s all in one file.

Read: LibInject 0.1.0: Small Pieces Smooshed Together

Topic: Ruby on Rails podcast Previous Topic   Next Topic Topic: Fun with File.join

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use