Rakefile - an access variable defined in the premise
Is there a way to access the variable defined in the prerequisite? eg.
task :new_file do
filename = 'foobar.txt' # in reality I ask the user for the filename
File.write(filename, 'Some glorious content')
end
task :new_file! => [:new_file] do
exec "vim #{filename.inspect}"
end
I would like it to rake new_file!
just be a shorthand for rake new_file
along with running vim for any file I create in the task new_file
.
All I can imagine is populate a global variable FILENAME
in :new_file
and use it in new_file!
and then clear it, but if there is a "bigger rake" way I would like to know.
+3
source to share
1 answer
One way is to define a variable outside of such tasks:
filename = 'default.name'
task :new_file do
filename = 'foobar.txt' # in reality I ask the user for the filename
File.write(filename, 'Some glorious content')
end
task :new_file! => [:new_file] do
# filename will be visible here too, and its value was set in new_file
exec "vim #{filename.inspect}"
end
0
source to share