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

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"

Related

How run the ``rspec --init` for a sub-directory ?`

I'am trying to write a simple ruby code supposed to automate tasks. One of the task is initializing the rspec gem for a directory which is not the current directory.
I made first tries directly in the terminal before writing my code in he ruby file. Here is what I have tried (in a terminal window) :
$rspec --init a_sub_directory
OR
$rspec a_sub_directory --init
The result in both cases : all the rspec elements ( .rspec + spec/spec_helper.rb) are created in the current directory and the sub directory did not change its content.
Is there anything possible to get the rspec elements created in a sub directory of the current directory ?
Use the Dir.chdir method to change the current directory and then execute rspec --init. It could be :
Dir.mkdir("a_sub_directory")
Dir.chdir("a_sub_directory")
system("rspec --init)
And that's it; I've tried this, it works.

Creating a batch file that runs a Ruby script

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

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.

How to open a file at the same folder of the ruby script?

The following .rb script runs fine if excuting at the script's folder:
db = YAML::load(File.open('db.yml'))
ActiveRecord::Base.establish_connection(db)
The File.open will fail if the script is running outside the script folder. How can I supply the script's path to db.yml? Thanks!
This should work:
db_file = File.join(File.dirname(__FILE__), "db.yml")
Edit: I got a little bit confused with the script folder, this should work now.
If you find yourself wanting to do this a bunch, you might consider adding the script's directory to your load path (especially in 1.9.2 where "." is no longer in the load path):
$: << File.expand_path(File.join(File.dirname(__FILE__)))

How to get the current working directory's absolute path in Ruby?

I'm running Ruby on Windows though I don't know if that should make a difference. All I want to do is get the current working directory's absolute path. Is this possible from irb? Apparently from a script it's possible using File.expand_path(__FILE__)
But from irb I tried the following and got a "Permission denied" error:
File.new(Dir.new(".").path).expand
Dir.pwd is the current working directory
http://ruby-doc.org/core/Dir.html#method-c-pwd
File.expand_path File.dirname(__FILE__) will return the directory relative to the file this command is called from.
But Dir.pwd returns the working directory (results identical to executing pwd in your terminal)
As for the path relative to the current executing script, since Ruby 2.0 you can also use
__dir__
So this is basically the same as
File.dirname(__FILE__)
This will give you the working directory of the current file.
File.dirname(__FILE__)
Example:
current_file: "/Users/nemrow/SITM/folder1/folder2/amazon.rb"
result: "/Users/nemrow/SITM/folder1/folder2"
Through this you can get absolute path of any file located in any directory.
File.join(Dir.pwd,'some-dir','some-file-name')
This will return
=> "/User/abc/xyz/some-dir/some-file-name"
If you want to get the full path of the directory of the current rb file:
File.expand_path('../', __FILE__)

Resources