title: Ruby: Accessing rake task from a gem without Rails
body: <p>I'm aware that Rake tasks can be defined in a number of places within a Ruby gem:</p>
  <ul>
  <li>inside a Rakefile</li>
  <li>inside <code>tasks/*.rake</code></li>
  <li>inside <code>lib/tasks/*.rake</code></li>
  </ul>
  <p>I've read that the first two should be used when the tasks are to be executed on the gem itself. It seems the third option should be chosen when tasks are desired to be publicly available.</p>
  <p>There are many tutorials online demonstrating a variety of methods to load Rake tasks from a gem using Rails, namely by utilising <code>Rails::RailTie</code>.</p>
  <p>However, I'd like to find a way of using a dependency gem's tasks within another gem without needing Rails.</p>
  <p>Is there a simple solution to this? Would someone be kind enough to describe the proper approach, or outline what approaches would be viable?</p>
  <h3>UPDATE</h3>
  <p>I've tried creating a file <code>bin/my-gem</code> to make available on the system for executing Rake tasks from <code>my-gem</code>. I've put the following inside;</p>
  <pre><code>#!/usr/bin/env ruby
  require 'rubygems'
  require 'rake'
  task=ARGV[0]
  spec = Gem::Specification.find_by_name('dsi_core')
  Dir[&quot;#{spec.gem_dir}/lib/tasks/*.rake&quot;].each {|file| puts file and Rake::load_rakefile(file)}
  Rake::Task.clear # Avoid tasks being loaded several times in dev mode
  Rake::Task[task].reenable # Support re-execution of a task.
  Rake::Task[task].invoke
  </code></pre>
  <p>Some of the content was based on <a href="https://stackoverflow.com/questions/1170148/run-rake-task-in-controller">this SO post</a>.</p>
  <p>Sadly I'm doing something wrong because upon installing the gem then running <code>my-gem mytask</code> with <code>mytask</code> defined in <code>lib/test.rake</code> then the following is output:</p>
  <pre><code>/var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task_manager.rb:49:in `[]': Don't know how to build task 'mytest' (RuntimeError)
      from /var/lib/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:298:in `[]'
      from /var/lib/gems/1.8/gems/my_gem-0.0.1/bin/my_gem:8
      from /usr/local/bin/my_gem:19:in `load'
      from /usr/local/bin/my_gem:19
  </code></pre>