See what gems are currently included in current ruby/irb session

(Saturday, June 11, 2011, at 09:22 AM)

So, part of 140kit requires that we dynamically load any arbitrary gems. It’s basically done with the following code:


  def self.method_missing(method_name, *args)
    analytical_offering_functions = AnalyticalOffering.all(:language => "ruby").collect{|ao| ao.function}
    if analytical_offering_functions.include?(method_name.to_s)
      if self.respond_to?(method_name.to_s+"_dependencies")
        dependencies = Analysis::Dependencies.send(method_name.to_s+"_dependencies")
        dependencies.each do |dependency|
          require dependency if !Gem.loaded_specs.keys.include?(dependency)
        end
        puts "Required #{dependencies.length} dependencies:" if !Gem.loaded_specs.keys.include?(dependency)
        puts dependencies.join(", ") if !Gem.loaded_specs.keys.include?(dependency)
        return dependencies
      else
        return "No dependencies defined."
      end
    else
      super
    end
  end

Where method_name is the name of an analytical process included in the ‘kit. This way, you can write some analytical process, stipulate what gems are needed for the process to run, and its automagically required, updated, maintained, and set up for the analytic to use. One small annoyance, however, is that whenever it re-runs this analytic, it was never smart enough to just ask if that gem had already been included. So, here’s the new line that is added (you see it on three different lines, because i was copy-pasta’ing, and I really like the line so its fun to see it…)


Gem.loaded_specs.keys #returns hash of currently loaded gems, keys are the names ('fastercsv','hpricot', etc..), values are the files loaded in order to run it, along with various internal gem values.
Gem.loaded_specs.keys.include?("fastercsv") #is fastercsv already included?

So thats it!

Back