Ruby Environment Variables? - ruby

I'm looking into learning some Ruby, I've installed it on Windows 7 X64 using the installer you can find at RubyInstaller.org .
Now my problem is that the installation is done and all that. But when I try and check for the ruby version in command prompt e.g
ruby -v
I just get an error saying that my system doesn't know what that is. Including ruby -e and ruby -s etc... Now coming from a Java background I assume that this has something to do with environment variables that might not be set for Ruby?
How exactly does one go about this to quickly get set up?

Try to edit your PATH variable. My PATH variable contain C:\Ruby192\bin, in this folder there are files ruby and irb.
Also I think there is an option for that in the installator, which you may check.

Find Ruby installation path. Maybe something like "C:\Ruby193...". There is a bin dir under that.
Add the bin dir to your PATH env.

The advice to check the PATH option on install is good.
Another thing that helped me was searching Windows for "Start Command Prompt with Ruby." From that command line, Ruby worked well and I am able to watch my XAMPP directory for SASS changes. I was never able to get that to work using either Git Bash or the regular Windows command line, but the Command Prompt with Ruby worked like a charm.

Related

I want to make a shell command program in ruby for windows but everything is in linux

I have a ruby program named options that I want to run from the command line with a few options like
options -add 400
options -sub 600
options -h
I am already using optparse to interpret the commands but I want to run the program as its own script, but have to run it as
ruby options -add 400
I've looked it up and found some answers like How to create a shell command supporting Ruby? which seems to be linux and I'm not sure of part of the explanation like which bin to put in, or answers like Shell execute from Ruby whose answer still requires ruby in the command. Can anyone explain how to do this in more depth, or direct me to a source that explains it without relying on a linux platform?
Assuming that you have ruby in your PATH on Windows couln't you just create wrapper script and place it in folder present at your PATH?
ruby options %*
Do you have thought about to make it as gem ? There is a simple guide http://guides.rubygems.org/, then you can make the gem which will be working on linux and windows too. I don't know if you know but when I did software avaible under shell or bash I used slop (https://github.com/injekt/slop) it is very convenient the gem. Please see the source of slop, because it is an example of gem, all structure and necessary files to build the gem.

RVM on Ruby Scripts

I need for a Ruby script to be run using an rvm-selected version. I cannot change how the script is invoked, but I can modify the script. The script starts with:
#!/usr/bin/env ruby
Now, based on some information I found (in this question, for example), I tried this:
#!/usr/bin/env rvm-shell ree-1.8.7-2012.02#gitorious
But this only gives me this error message:
/usr/bin/env: rvm-shell ree-1.8.7-2012.02#gitorious: No such file or directory
Now, rvm is available, because this works (but doesn't bring the required ruby/gemset):
#!/usr/bin/env rvm-shell
I've tried this as well:
#!/usr/local/rvm/bin/rvm-shell ree-1.8.7-2012.02#gitorious
But this doesn't bring in the environment ("gem", which is only installed inside that gemset, is not available, for example). If I run that on the command line itself, it does open a shell with the proper environment.
So, has anyone done something like this? How can I fix it?
Does this work?
#!/location/of/rvm/folder/rubies/ree-1.8.7-2012.02#gitorious/bin/ruby

Ruby 1.9.3 #OSX Lion and Cron

I installed Ruby 1.9.3p125 via this guide (up to point #5): LINK
Now I have this problem: my script works wonderfully from my command line, but if I execute it from Cron it seems to use a default environment and defaults to /usr/bin/ruby instead of mine (~/.rvm/rubies/ruby-1.9.3-p125/bin/ruby). What is the best way to have executed commands - manually or via cron - produce the same results?
PS: It seems to skip processing ~/.bash_login for example, where rvm is loaded into PATH
In your crontab line, you can source the .bash_login before you script is run.
source ~/.bash_login && <your original command here>
That way your script will have everything you have when you run it.
The usual way recommended to do this would be to put the full path to the executable in your crontab. E.g.
crontab should show:
/Users/Poochie/.rvm/rubies/ruby-1.9.3-p125/bin/ruby /full/path/to/script.rb
or whatever the full path is. It's much more robust than trying to get rvm loading, as here an rvm script is modifying your path for you. If you want to set it to whichever is the rvm default ruby (e.g. whatever was set by rvm use x.x.x --default), You can use: /Users/Poochie/.rvm/bin/ruby as the executable instead, e.g.:
/Users/Poochie/.rvm/bin/ruby /full/path/to/script.rb
I actually found this post which helped me a lot: LINK
I managed to run my script as I wanted but the questions is theoretically still open because the issue could still affect cron usage in general.

RVM | Running user scripts

Is it possible while using the Ruby version manager to run scripts not from the console but using other ways — at system startup or by a keyboard shortcut for example?
RVM installs a command rvm-shell. You can use rvm-shell, pass it whatever you would pass rvm use, then you can execute a shell command.
rvm-shell will set your environment for that shell script, or you can use rvm-shell on one line, and have it execute the parameter as a shell command.
For example:
rvm-shell rbx-2.0 -c 'which ruby'
Which should equal your rbx ruby.
It'd help to know the system, but the answer is yes, though you'd need to either know which is the current directory, or set the system ruby to be the one you needed for these scripts (especially startup). You might also need to experiment, as it would depend at which point in the startup you needed the scripts to run, but you can probably get more answers on that from the irc rvm channel.
rvm default do /path/to/ruby/script

Only one certain user can run Ruby scripts

Whenever I try to run a Ruby script as any user but myself, I get this error:
/usr/bin/env: ruby: No such file or directory
I don't understand why this should be the case. When I installed Ruby, why would it think I only want it for one user?
Here's this if it helps:
$ which ruby
/home/jason/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
RVM defaults to only install for your own user account. However, it looks like they provide instructions for a system-wide installation which will allow access to all users. (However, each user will still need the RVM-specific updates in their shell profiles).
http://rvm.beginrescueend.com/deployment/system-wide/
Maybe someone else will have a better answer but here's something I came up with:
The first line of my script was this:
#!/usr/bin/env ruby
I changed it to this and now other users can run the script:
#!/usr/bin/env /home/jason/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
I think the other users could be missing /home/jason/.rvm/rubies/ruby-1.9.2-p136/bin in their PATH environment variable, so /usr/bin/env can't find ruby. Check their PATH and see if that is the case.

Resources