Skip to content

Instantly share code, notes, and snippets.

@guillermo
Created September 15, 2011 20:58
Show Gist options
  • Save guillermo/1220466 to your computer and use it in GitHub Desktop.
Save guillermo/1220466 to your computer and use it in GitHub Desktop.
22:55:19 guillermo@leuschnerdamm:/tmp/asdf ruby-1.9.2-p290
$ rake install
Installing
Installing
22:56:07 guillermo@leuschnerdamm:/tmp/asdf ruby-1.9.2-p290
$ rake install2
WADUS WADUS
22:56:09 guillermo@leuschnerdamm:/tmp/asdf ruby-1.9.2-p290
$
The second method is only possible with newer version of rake. Old ones use top level binding to execute code.
# Require always use TOPLEVEL_BINDING
# so next method will be there
def install
puts "WADUS WADUS"
end
# Option 1
# The clean one, you can move the class to other file
module Utils
extend self
def install
puts "Installing"
end
end
desc "We are going to install... LA LA LA"
task :install do
Utils::install
Utils.install # You can use whatever you want
end
# Option 2
# You can use a separete file.
$:.unshift(File.dirname(__FILE__)) # current dir is not included
require 'my_file'
desc "We are going to install... LA LA LA"
task :install2 do
TOPLEVEL_BINDING.instance_exec{ install }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment