I am doing a demo command line project in Ruby. The structure is like this:
(other scripts and files)
You can't do this with file permissions, since the user needs to read the files; removing the
read permission means you can't include it either. Removing the
execute permission is useful to signal that these file aren't intended to be executed, but won't prevent people from typing
The easiest way is probably to set a global variable in the
#!/usr/bin/env ruby FROM_INIT = true require './incl.rb' puts 'This is init!'
And then check if this variable is defined in the included
unless defined? FROM_INIT puts 'Must be called from init.rb' exit 0 end puts 'This is incl!'
A second method might be checking the value of
incl.rb; this stores the current program name (like
argv in many other languages):
unless $PROGRAM_NAME.end_with? 'init.rb' puts 'Must be called from init.rb' exit 0 end
I don't recommend this though, as it's not very future-proof; what if you want to rename
init.rb or make a second script?