Temporary Environment Variables in Ruby - ruby-on-rails

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.

Related

How do I update my zsh prompt when an env variable changes?

I'm using oh-my-zsh. My theme file looks like this:
PROMPT="${AWS_PROFILE}%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
It's based on a popular theme. The only thing I have added is ${AWS_PROFILE}.
The prompt does show my AWS profile as intended. But even if I the environment variable changes the prompt doesn't change. This is not as I had intended. It does change if I run source ~/.zshrc.
Can I make the prompt update when my env variable changes?
Copying #chepner's comment into an answer:
AWS_PROFILE is expanded immediately when you define PROMPT, since you used double quotes. Use single quotes, or consider using a precmd hook to set the value of PROMPT.

ZSH `rvm-prompt` on RHS prompt not refreshing

I have rvm-prompt feeding into my RPrompt, however it is not refreshing between commands: (Larger image)
For example, when I cd from one ruby project to another with an .rvmrc file pointing to a new gemset, the rprompt simply will not refresh. It appears that it must be caching the rprompt for performance purposes, so I am curious as to how I can force a refresh for zsh at each command?
How do you generate the prompt? I do it like this:
local rvm_ruby=' %{$fg[red]%}[$(~/.rvm/bin/rvm-prompt i v g s)]%{$reset_color%}'
And then use the rvm_ruby variable in my prompt:
PROMPT="${user_host}${directory}${git_branch}${rvm_ruby}%B
→%b "
EDIT: Note that the place where you create the contents for the variable needs single quotes, otherwise the command will get substituted right away and not updated anymore. This initially took me a bit to figure out. You may have the same problem defining your RHS prompt.

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}`

Change directory using os:cmd/1

I am trying to change the directory in the command line from a gen_server using
os:cmd("cd d:\temp").
but nothing happense, the return is just an empty list and I remain in the same directory.
any ideas?
Try using file:set_cwd(Dir) to change your current dir.
cmd() runs a sub-shell, which you're telling to change directory, then the sub-shell exits, having changed nothing about its parent process's environment.
You want to use cd() instead, if you're in the shell, or file:set_cwd() at runtime within an Erlang program.
Another option, if you want to run another program and have its working directory be different from the one Erlang is using is to pass the {cd, Dir} tuple to open_port().

Resources