Creating a batch file that runs a Ruby script - ruby

I want to create a batch file which can run a Ruby file like test.rb.
Anyone have any idea how to create one?
Im using Ruby 2.0.0 and Windows 10.

You just need to do this.
cd location
test.rb
This might also work.
cd ruby_install_location
ruby.exe c:/fullpath/test.rb

Related

List Ruby file in the directory using interactive ruby (irb)

I am new to ruby language. I want to list the ruby file in the folder. When I use ls, the following error comes up. (By the way, I use pc, not Mac. What command line should I use to get ruby file in the directory? Thanks!!!
To list the ruby files (files with .rb extension) using ruby code you can use Dir.glob
Dir.glob('*.rb')
The documentation has other examples, such as getting all files with .rb extension in nested folders:
Dir.glob('**/*.rb')
If you are in the working directory you want to list files in, you can execute this in your ruby code:
Dir.entries(".")

How do I call a shell script which calls python scripts from a different folder?

Right now, I have shellScript.sh and test_me.py in a folder ABC/def. shellScript.sh calls test_me.py. I'm trying to call shellScript.sh from the ABC folder. So far, I keep on getting "No such file or directory" errors.
I've tried calling the python script from the shell script such as:
python /ABC/def/test_me.py
but this still gives me the same error.
How do I fix this?
Make sure your home directory starts by a capitalize H, if it does not (which is highly probable) your script won't work.
If the problem is not in the path or filename, then it could be in the way you are executing the shellScript.sh:
./shellScript.sh
might fix it provided you have the proper execute bit set on the script file. If not, try this:
sh /proper/path/to/shellScript.sh
If that does not fix it, then try to cd to the directory in the script before the python line:
cd /to/proper/folder
python test_me.py

How to run cd command/change the folder path from ruby script

I am trying to run a command from ruby script. I got stuck in changing the folder path. Below is the command that I wrote. Can anyone let me know how to go ahead?
system("cd /home/user/Source/pxe/")
I want the terminal to point to the folder pxe when I run the ruby script. Is the code above correct? If not, can you let me know what the correct way is to call the cd command from ruby script?
Do you want to change current directory for the script? Use Dir.chdir.
Dir.chdir('/home/user/Source/pxe')
You can also use FileUtils#cd method.
Changes the current directory to the directory dir.If this method is called with block, resumes to the old working directory after the block execution finished.
Example( I am on windows-7) :
require 'fileutils'
Dir.pwd # => "C:/Program Files/Notepad++"
FileUtils.cd("C:\\Users\\rakshiar\\Downloads")
Dir.pwd # => "C:/Users/rakshiar/Downloads"

How to make Ruby file run as executable?

I want my Ruby Script File to run as executable from any directory of Windows XP. I have created a test.rb (Ruby Script file) and want to run it from any directory of my Windows as "test" for example, "C:\test" or "C:\Directory\test" runs my file test.rb.
#!/usr/bin/envy ruby
p "Hi this is my test file"
I have added the shebang code in my ruby file but when I have to run the Ruby Script, I have to locate my Script file and run it expicitly as "ruby test.rb".
I have also made the file executable by executing the command:$ chmod +x hello-world.rb
, but it still does not work.
Thanks in advance.
I assume you're using Linux or OS X and creating the file on a disk accessible from Windows? Windows does not use shebangs, and it does not use Unix file modes. You will need to associate files with the .rb extension to the Ruby executable; details for that operation can be found at this Stack Overflow question; once you have done this, you can run C:\whatever\test.rb or C:\whatever\test to execute the script.

Terminal not executing Ruby files

I'm pretty much entirely new to programming, so bear with me. I am on a Macbook Pro running 10.6.6.
At the beginning of the "Learn to Program" tutorial are these directions:
Save your program (yes, that's a program!) as calc.rb (the .rb is what we usually put at the end of programs written in Ruby). Now run your program by typing ruby calc.rb into your command line. It should have put a 3 on your screen. See, programming isn't so hard, now is it?
When I type ruby calc.rb I get the following error:
ruby: No such file or directory -- calc.rb (LoadError)
What am I doing wrong? "Calc.rb" is saved on my desktop, if it matters.
If you have named your file "Calc.rb" you'll need to run ruby Calc.rb (capital "C").
Also make sure that you're navigating the /Desktop directory — e.g. type
cd ~/Desktop
ruby Calc.rb
You can use ls to get a list of all the files in the current directory. You could use that to see if the file you're trying to execute actually exists.
In Terminal, did you change your current directory to your desktop before running your program ?
If your program is stored on your desktop, you first need to change directory to your desktop or your Ruby script will not be found.
$ cd Desktop
$ ruby calc.rb
ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
C:\Ruby Programs>ruby calc.rb
ruby: No such file or directory -- calc.rb (LoadError)
C:\Ruby Programs>ruby calc.rb.txt
3
One should not have to add the .txt extension while attempting to run the program from whithin the program's home folder. I have to though.
Did you call your file Calc.rb and saved it as a rb file to. becouse then its called internaly. Calc.rb.rb
and be sure to run cmd from desktop :D
You need to make sure you're running the command in the same directory you have the calc.rb file.
Type "cd " then the directory calc.rb is in to change it.
Then just do "ruby calc.rb"

Resources