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.
Related
I have these lines on top of my Ruby code, and tried multiple combinations but none of them have worked.
$:.unshift File.dirname($0)
Dir.chdir(File.dirname($0))
I have a config file that is in the same directory than the exe created by Ocra. The file is loaded by this:
cnf = YAML.load_file('config.yml')
However, the file doesn't load from the same directory as wanted. The error, I think, tells it tries to load it from the temporary directory when the exe runs.
How can I get the script load the config.yml file from the same directory than the exe?
Ocra uses an environment variable to store the location of the .exe
ENV["OCRA_EXECUTABLE"]
To access files relative to where your ruby_script.exe is you have to change your working to there. Here is some code that may work for you:
Dir.chdir File.dirname(ENV["OCRA_EXECUTABLE"]) if ENV["OCRA_EXECUTABLE"]
Having the "if ENV["OCRA_EXECUTABLE"]" at the end of this line keeps the script from throwing an error when your it is running without ocra as a ruby file (.rb). It simply checks if this ENV exists, if so then your program is running inside your ocra EXE
So I am writing a gem that runs through the command line to learn how to count cards. How do I set the file structure so that the file is run and get get input from the user?
For example what runs the function that makes the code work is a ruby file that contains only the following:
require "cardcounter.rb"
CardCounter.run_program
Is there a way that when the user downlaods my cardcounting gem, they can just type cardcount and it would run CardCounter.run_program, without having to be in irb or anything?
You need to create a bin directory in your gem's file structure and put the relevant code there (in your case in cardcount file).
Read more here.
I am learning rspec and cucumber from the book - The Rspec book. This question is not about those two things. It is more about linux. My book gives me instructions which make no sense. Please help me to understand it.
Create a bin in the project root directory (sibling to lib and spec), and add a bin/codebreaker file. If you’re on a *nix system, enter this code in that file:
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.expand_path( '../../lib' , __FILE__)
require 'codebreaker'
game = Codebreaker::Game.new(STDOUT)
game.start
Shouldn't that be a bin/codebreaker folder ? Anyway, I made a folder bin, under project root. I also made a file bin/codebreaker.rb with the above code and continued.
If you’re on *nix, now run chmod +x bin/codebreaker so we can execute
it, and then run this:
$ bin/codebreaker
Welcome to Codebreaker!
Enter guess:
Now look at that! Who knew that this little bit of code was actually going to start to make something work?
I don't get the above output when I go to project root and execute bin/codebreaker from there. I only see this output -
bash: bin/codebreaker: Is a directory
Am I missing something ? How do I make this work ?
The instructions tell you to create a file named codebreaker. However, you created a file named codebreaker.rb, not codebreaker. In addition to the file named codebreaker.rb which you weren't supposed to create, you also created a directory named codereaker which you also weren't supposed to create.
I don't know CodeBreaker, but the instructions you posted should work, although they are a little ugly (e.g. manually fiddling with $LOAD_PATH instead of simply using require_relative). Just follow them. Or if you do deviate from them as you did with naming your file codebreaker.rb instead of codebreaker, then you must of course also adapt all further instructions to that change, e.g. for example running bin/codebreaker.rb instead of bin/codebreaker.
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"
I'm a new vim user who is looking for a little help with a script that would allow me to call mvn build.
Currently I map this as follows
map <F3> :! mvn build<CR>
but because this tries to do the build in my current working directory (currently java development so I'm deep in a package under src usually)
How might I go up the directory structure so this mvn build command works correctly?
Thank you in advance
The vimscript function finddir() can be used to find the src directory. The syntax used in its second argument (path) has some augmentations to basic path specification, one of which is the ability to specify an upward search using the ;:
let src_dir = finddir('src', ';')
This will find the src directory if it's above the current directory.
To do a build in this directory, you can run
exec '!cd' shellescape(src_dir) '&& mvn build'
I don't think this will work under windows. In that case, I think you'd want to change directory temporarily using vim's cd command, run the command, and then change back using :cd -.
You can combine those commands into a function
function! RunMavenInSrcDir()
let src_dir = finddir('src', ';')
exec 'cd' fnameescape(src_dir)
!mvn build
cd -
endfunction
Running this function will have the side effect of clobbering vim's previous current directory: in other words, doing :cd - afterwards won't necessarily change to the same directory it would have before.
You can arrange to have the function invoked by a mapping with
map <F3> :call RunMavenInSrcDir()<CR>
or by a command with
command! RunMavenInSrcDir call RunMavenInSrcDir()