I have a Ruby file that has a line as such:
abort "ID is less than 1!" if env_id.to_i < 1
I am requiring this Ruby file elsewhere and can't pass a parameter when requiring this file. So I keep getting an error where when I try to require the file it aborts with "ID is less than 1!" The env_id is a variable that will be used for uniquely defining a bunch of AWS instances.
Any ideas as to what to do here?
I figured out how to solve my problem. I am just going to use FILE to see if the file is being run directly or required.
Related
In C/C++ you can use __FILE__ and __LINE__ to get access to the current file and line number.
Does Go provide something similar?
Indeed it does:
http://golang.org/pkg/runtime/#Caller
runtime.Caller can also be used to get the file name/line number of calling functions, too.
I am doing a demo command line project in Ruby. The structure is like this:
/ROOT_DIR
init.rb
/SCRIPT_DIR
(other scripts and files)
I want users to only go into the application using init.rb, but as it stands, anyone can go into the sub-folder and call other ruby scripts directly.
Questions:
What ways can above scenario be prevented?
If I was to use directory permissions, would it get reset when running the code from a Windows machine to on Linux machine?
Is there anything that can be included in Ruby files itself to prevent it from being directly called from OS command line?
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 ruby incl.rb.
The easiest way is probably to set a global variable in the init.rb script:
#!/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 incl.rb file:
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 $PROGRAM_NAME in incl.rb; this stores the current program name (like argv[0] 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?
Running the command ocra script.rb --no-autoload --no-enc --add-all-core gives me the error initialize: can't convert nil into String (TypeError) for the following line:
doc = Nokogiri::XML(File.open(ARGV[0]))
Whats going on here? I want to build the executable to be able to take any argument and use that file as the xml configuration.
It seems a long time but the accept solution doesn't work for me.
The working solution is adding -- then any fake data to your argument to make the execution flow to be just as normal
example for:
so you need to do
ocra yourscript.rb -- ANYDATAHERE
Just add this above that line:
exit if defined? Ocra
# skip anything below this line when we're building the exe
Unless there's a require or otherwise loaded dependency below that line you should be fine.
This might be a trivial question, but I've searched and found nothing about it. I'm trying to make a complex application on ruby and I have my classes on different .rb files which the "Main" class requires. As it's written in the code:
require 'Book.rb'
require 'Person.rb'
These files that contains the classes are in the same directory, so what I'm looking for is a Batch command which allows me to include these files, like the one for one file but extended:
ruby Main.rb
Which, of course fails saying that it can't find 'Book.rb' (returns error and doesn't look for the other)
Thank you for your help.
Thanks to the user Abhi y found this post. The command that worked for me was:
irb -I . -r Main.rb
From which you can get further information on the link.
[I'm just starting with Ruby, but "no question is ever too newbie," so I trudge onwards...]
Every tutorial and book I see goes from Ruby with the interactive shell to Ruby on Rails. I'm not doing Rails (yet), but I don't want to use the interactive shell. I have a class file (first_class.rb) and a Main (main.rb). If I run the main.rb, I of course get the uninitialized constant FirstClass. How do I tell ruby about the first_class.rb?
The easiest way is to put them both in the same file.
However you can also use require, e.g.:
require 'first_class'
You can also use autoload as follows:
autoload :FirstClass, 'first_class'
This code will automatically load first_class.rb as soon as FirstClass is used. Note, however, that the current implementations of autoload are not thread safe (see http://www.ruby-forum.com/topic/174036).
There's another point worth noting: you wouldn't typically use a main file in ruby. If you're writing a command line tool, standard practice would be to place the tool in a bin subdirectory. For normal one-off scripts the main idiom is:
if __FILE__ == $0
# main does here
# `__FILE__` contains the name of the file the statement is contained in
# `$0` contains the name of the script called by the interpreter
#
# if the file was `required`, i.e. is being used as a library
# the code isn't executed.
# if the file is being passed as an argument to the interpreter, it is.
end