How to reference a relative path in Rails? - ruby-on-rails

In my controller, a function runs a shell command. I'm having a hard time to locate the script in the commad. For example, in my controller I have this:
def find_usage
command = 'ruby usage.rb'
#{command}
end
My question is where to put that usage.rb file and how to reference it without hard code the entire path. It works when I hard code the path as /home/user/xyz/usage.rb, but I don't think it's right to do.

You could stuff it into /script and then reference RAILS_ROOT/script/usage.rb in your code. RAILS_ROOT is a constant that contains the absolute path to your application.

Related

Temporary Environment Variables in Ruby

Try to bear with me as I am fairly new to this and don't have much coding experience.
Im trying to use a ruby script to add a location to my PATH variable. So far I just have
path = ENV['PATH'].to_s
ENV['PATH'] = path + ";" + location
print ENV['PATH']
The problem is that the file seems to be added to the PATH and prints with it, but when I go check my path variable the new location is not there.
Also, when I run a separate script of which is one line:
print ENV['PATH']
the new location is not there either.
My question is is there a way to make the new PATH "save" instead of reverting to the old PATH when the script is finished?
If i am not mistaken you cannot really edit the environment variables.
When loading your script ruby loads all the currently known environment variables and adds the values to ENV.
When editing it, it will only be changed temporarily for the current execution.
If you want to change it and want it to persist you will have to e.g. use system
system("export PATH=$PATH:YOUR_PATH")
Same as you would do it in the CLI
The best you can do is generate a shell command to be evaluated outside the Ruby script and inside the shell you are running.
Something like this should work:
puts "export PATH=#{ENV['PATH']};#{location}"
Then, in the shell you do
eval $(ruby_script)
However, since you seem to want to run this in Windows you probably want to use command substitution, in which case you output the location directly:
puts location
And in the Windows shell:
set PATH=%PATH%;(ruby_script)
colon ":" is the field separator for PATH not semicolon ";" in Unix. Your example worked just fine for me today when I changed the semicolon to a colon.

Lua: require fails to find submodule, but searchpath succeeds?

usually when I have a question about something remotely software related I find that someone else has already asked the very same thing, and gotten good answers that works for me too.
This time, though, I've failed to find an answer to my predicament.
Here we go:
I'm currently trying to move up my Lua-programming a notch or three and want to use modules. So, I've got a structure like this:
main.lua
foo/bar.lua
Now, in main.lua I do
require("foo.bar")
which fails,
main.lua:1 module 'foo.bar' not found:
no field package.preload['foo.bar']
no file 'foo.bar.lua'
no file 'foo.bar.lua'
no file 'foo.lua'
Ok, something might be wrong with my package.path so I use package.searchpath("foo.bar", package.path) to see what I', doing wrong.
The problem is that package.searchpath resolves foo.bar to foo/bar.lua which is exactly right.
As I've understood it, package.searchpath tries to find the module in the same way as require, but there seems to be som glitch in my case.
What strikes me as odd is the repetition of the no file 'foo.bar.lua' in the error output
Have I misunderstood the use of require?
I'm using LuaJIT-2.0.0 to run my chunks
Update:
I'm using LuaJIT-2.0.0 to run my chunks <- This was the reason for my problem, stock Lua-5.2.2 behaves as expected
package.path = debug.getinfo(1,"S").source:match[[^#?(.*[\/])[^\/]-$]] .."?.lua;".. package.path
require("foo.bar")
This line causes require to look in the same directory as the
current file when asked to load other files. If you want it to instead
search a directory relative to the current directory, insert the
relative path between " and ?.lua
Here is part of require description:
[...] Otherwise require searches for a Lua loader using the path stored in
package.path. If that also fails, it searches for a C loader using the
path stored in package.cpath. If that also fails, it tries an
all-in-one loader (see package.loaders).
Default path for package.path is always the .exe that executes specified script.

Escript: setting code path relative to script directory

When I try to set a relative code path in a escript with -pz like this
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -pz ../dir-of-some-beams
The path is interpreted relative to the directory from where I run the escript from, which renders it useless for setting the path relative to the script location.
My current "workaround" is using a absolute path which is annoying since all this is part of a repository and I don't want it to be location dependent.
So how can I set the code path relative to the directory the escript is located in?
Just found it out myself:
At the beginning of main add code like this:
true = code:add_pathz(filename:dirname(escript:script_name())
++ "/../dir-of-some-beams"),
I recommend always testing for true whith these code functions, because its easy to type code:add_pathsz which wants a list of strings and always returns ok, even if you pass it just a string -- but it doesn't set the code path to the single directory (which is pretty annoying behaviour btw).

directory path question

What is the difference between:
include("./somepath/class.php");
and
include("somepath/class.php");
There shouldn't be any difference, directory wise, since the former assumes relative directory structure. The only potential pitfall could be if "somepath" were actually a command to be run - some users expect to type a command for a local script file and assume it should run, when you actually have to run "./somepath" to invoke it. This, of course, only pertains to commands not on your $PATH.
I don't see any difference. "." means current directory.
. refers to the current working directory.
Sometimes you want to specify explicitly that what you want is in the current working directory, and that it is not from something in your path variable for example.
For example in PHP "somepath/somefile" is appended after paths specified in include_dir (for example: /home/var/; /home/bin/ ....) directive.
The second variant is more specific it says: search in current directory!

Running C processes in Rails

I make a call just like this:
value = ./simulated_annealing
Which is a C Object file, but Rails tells me it cannot find that file. I
put it in the same dir that the rest of the models files (since it's
called by one of those models), but I guess it should be in any other
place.
I've tried that outside Ruby and it works great.
What do I have to do?
The thing is, when you say:
./simulated_annealing
you're explicitly saying: run file named simulated_annealing which is found in the current directory. That's what the ./ means. If the file's located elsewhere you need to provide the path to it, or add that path to the environment variable $PATH. So, you should replace that line with:
/path/to/simulated_annealing
where /path/to represents the actual path.
The best option is to use an absolute path for running the program. For ex.,
you can create a directory "bin" under your rails application top level
directory. Place your program under "bin" directory. Then you can
execute the program something like:
cmd = "#{RAILS_ROOT}/bin/cbin arg1 arg2"
value = `#{cmd}`

Resources