How to get files and directories set up for Ruby? - ruby

I am doing prep work for App Academy but I am having a hard time setting up files/directories to be able to run everything correctly. I have a couple questions and haven't been able to find concrete answers:
How do you make a directory in the windows terminal?
How do you run files previously written in Notepad in IRB? I wrote some simple scripts in Notepad because Sublime was causing me severe migraines. Or how do you change Notepad files to Ruby files?
How do you create new files in IRB like test_code.rb?

1) When at the Windows command prompt and having navigated to your working directory, type md directory_name
2) Unless you have specifically told notepad otherwise, notepad will have saved your file as a .txt file. You will simply have to rename the file extension from *.txt to *.rb.
3) When you installed Ruby on your Windows machine, the Ruby interpreter would have undoubtably been added to your path, so you should just be able to run your *.rb file direct from the windows cmd prompt and it will execute.
To load it while in IRB: Make sure you run IRB from the same folder as your *.rb file is in. Once you have cranked up an IRB session, type load 'my_file.rb'.

IRB is a great environment for testing code, but not for writing full scripts. Use Notepad or Notepad++ or Vim for Windows or your editor of choice, as long as it's capable of generating a text (non word processing document).
You can make a directory in the terminal or in the Explorer, it doesn't matter. Just note where you created it so you don't lose it.
If you want to run a script in Ruby, simply type ruby /path/to/the/file/script_to_run.rb and the Ruby interpreter should load and run the file.
You can load a script into IRB and watch it run, but that's rarely something we need to do. More often you'll want to run scripts using Ruby, and try things in IRB, since it's like a scratchpad.

Related

Running a selfwritten ruby program outside of an IDE

I was wondering if it was possible to run a selfwritten ruby program just like any other program by double-clicking an icon of some sort.
And if it's possible, how do I do it?
I wrote a little program for a friend but I don't want him to have to use the command line to run it, because that's rather inconvenient (unless there is a way to just double-click and the command line opens the program itself..).
Thanks for your help!
The simple answer that should work for all versions of Windows is to just create a simple batch launcher.
Create a .bat file. I usually just create a new .txt file via "right click > new > text document". Then rename it, highlight everything, including the extension, and rename it to something like run.bat. The .bat part is important. Once you rename it, the icon should change to gears. If you can't overwrite the extension, or Windows is still treating it as a text document, you'll need to either manually save it as a bat, or disable "hide file extensions" in the explorer settings so the extension can be changed.
Edit the bat file, and put into it something like:
#echo off
YOUR RUN COMMAND HERE THAT YOU WOULD NORMALLY TYPE MANUALLY
pause
Paste the command that you would normally run manually where the capital text is. The first line is so it doesn't repeat the commands back, and the pause is so if an error happens, the command prompt doesn't immediately close. This gives you a chance to read the error.
Save it and close it. Now, if you double click on the bat file, your program should run.
Multiple ways
if it's for occasional use and for one script only I would pack it
to a Windows executable with Ocra, then you can double click
the .exe itself or a link to it
same as above but use jRuby and create a .jar file, not for beginners though
the easiest: if you configure Windows to start/run .rb files with your ruby.exe you can double click the .rb files itself and they
will execute, they will have the red Ruby stone icon
if you run a .reg file to enable drap and drop on .rb files you can combine the previous technique to drop files on the script and
they will be the parameters to the script, see my answer here for the reg file
my favorite: copy the .rb to your windows "C:\Users\your_user\AppData\Roaming\Microsoft\Windows\SendTo\"
folder, then you can right click file(s) or folder(s) and select
sendto and select your script, the files or folder will again be the
parameters for your script
you can create a .bat or .cmd file that starts with the path to your ruby.exe and the script as parameter, use rubyw.exe if you
don't want output

RVM is not being used by Ubuntu Launcher

I'm trying to make my data files "clickable" like a MS Word document. When a user clicks on a file on the desktop with a ".vr" extension, I want it to execute my Ruby script, and pass the file name as a parameter.
I've created a mime type, "text/vr" with the ".vr" extension, and when I click the file, "text.vr" on the desktop, it attempts to run the command:
$vr test.vr
This command fails because it tries to use an old version of Ruby. The problem is that Ubuntu uses a different environment when it launches a program from the desktop instead of the prompt.
I use RVM, and it works perfectly when I write programs in the terminal. However, when I click on the file to launch the vr script, RVM isn't used. I need to find a way to make RVM load when a file is clicked.
What's going on here? What environment file is loaded when you execute a file from the desktop? ~/.bashrc? ~/.profile? How can I make it so my whole computer always finds RVM?
Thanks,
Eric
There are few ways, the environment for desktop is loaded from .profile, add one of this:
source $HOME/.rvm/environments/ruby-1.9.3-p327 #OR:
source $HOME/.rvm/environments/default
to load specific ruby.
sometimes it might not work with sourcing, then adding content of the default file to .profile should work:
cat $HOME/.rvm/environments/default >> ~/.profile
It is required to relogin / restart to apply the changes.

Running cmd scripts from a ruby file?

Is there anyway to write commands to the command prompt in windows and execute directly from a ruby program?
I would use this as a one click installer for all the gems I wanted to install on the computer after installing ruby. I hope that it would save time when transferring my ruby files to a new computer. Or would be an easier way to get a non-ruby person set up very quickly with all the gems I thought them might need.
I am imagining something like Watir but for the Cmd rather than a browser.
EDIT
Thanks to
How can I then close the cmd window without closing the program for instance:
'notepad'
starts a cmd window and it also starts notepad but the cmd windows stays until the notepad is closed.
Ruby will execute anything you put in backticks ` in your associated shell.
so if you type
test = `ipconfig`
puts test
test should now have stored in it the data from the cmd call ipconfig
EDIT
You can also use the System(..) call in Ruby to execute commands

Batch Scripts - Need to specify to use an ANSI console and execute a command

This is a noob question at its best but Google isn't finding what I need.
I have a ruby script that I need to fire off via task scheduler. I thought I did it right by using the following:
cmd /k ruby test.rb
This works when starting the .bat file from the folder. However, if it runs from taskeng.exe it fails because its looking in my system32 folder. I don't want it to run from system32 so how do i format this to run from say, c:/dev/
Again, sorry for the extremely noob question.
You can leave out cmd of that and just use
ruby test.rb
or rather (in your case):
ruby C:\Users\Foo\test.rb
or something like that. Giving the complete path to the script usually helps in finding it ;-)
In any case, if you need the working directory you can set it in the scheduled task itself. Something akin to the following:
       
Likewise, if you actually need cmd in there. Just fill out the Start in field and you have a working directory.

Shell Extension in the Command Prompt

I have a shell extension installed into the filesystem by adding ".{CLSID}" to the folder name. The Shell Extension is like the .zip extension (allowing you to see files). Explorer handles it fine, but how do I get other program to recognize the sub files/folder (programs like command shell). I navigate to the directory with the shell and it shows the empty folder (also, it doesnt strip away the clsid). Is there some way to implement that functionality? Do I have to install the extension in a different way?
Thanks!
chacham15
If I remember correctly, there is no documented way to add an extension to the windows cli shell (cmd.exe).
There is an add on shell for windows called 4NT that is extensible.
That said, you'll still end up writing scripts if I understand your question correctly.

Resources