Common Shell Commands in Ruby
For people who use linux a lot like myself they know very well,about how useful shell commands are, they can help us, to do a lot of amazing things in a short amount of time.
Few basic shell commands which every linux user performs are
cd (Change Directory)
- Which is used to change directory, cd home to change your working directory to home
- cd .. to go back one step in your directory tree
mkdir 'data' (make directory called data)
- Which is used to create folders or directories
pwd (print working directory)
- Which is used to get the current working directory
These few shell commands can help ruby programmers do some amazing things while writing ruby programs, when using them combined with Filing in ruby, we can create whole project structures in seconds, which can take several minutes when creating them manually.
Ruby helps execute few basic shell commands, via its Shell Class
lets see how can we execute them in our Ruby programs and create whole project structures :)
require 'shell'
_myshell = Shell.new()
## Current working directory
puts _myshell.pwd
## Current working directory
puts _myshell.dir
## Create a new directory called data
_myshell.mkdir 'data'
## Change directory
_myshell.cd 'data'
## Current working Directory
puts _myshell.pwd
## Go one step back in Directories
_myshell.cd '..'
## Check to see if directory exists
puts _myshell.exists?('data')
Ruby Project Structure
These few simple shell commands can help us create amazing things, lets take a look at this Ruby gem file structure. When you want to create a ruby gem, which you want people to make use of, you need to create it in a way that it follows this file structure, where you have one directory for putting your binary files, one directory for library files so on and so forth.
It can be very cumbersome to create such a structure manually, so we create ruby program, using in it Shell commands and Filing altogether to create such a project structure for us.
We can fill these files with some boiler plate code as well..
Take a look at this code down below, where we are creating a project structure for a dummy project .. you are not limited to a project structure which other people are using, you can be as creative as you can with your project file's structures .. I am just giving you some examples here, from which you can take inspiration.
require 'shell'
sh = Shell.new()
sh.mkdir "src" unless sh.exists?("src")
# make the 'src' directory if it doesn't already exist
sh.cd("src")
for dir in ["bin", "lib", "test"]
if !sh.exists?(dir)
sh.mkdir dir # make dir if it doesn't already exist
sh.cd(dir) do
# change to the `dir` directory
f = sh.open("#{dir}.rb", "w") # open a new file in write mode
f.print "## This is Test ruby program \n" # write to the file
f.print "module #{dir} \n"
f.print "\t ## code goes here \n"
f.print "end"
f.close # close the file handler
end
print sh.pwd # output the process working directory
end
end
Filing and Shell commands when combined can do amazing things :)
Notice that here, we are creating a folder called "src" and inside that folder we are creating three more folders, which are carrying one file each in them, which has a dummy module.
And here are the dummy modules, which this ruby program has created for us
Wow great work
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
@resteemator is a new bot casting votes for its followers. Follow @resteemator and vote this comment to increase your chance to be voted in the future!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
مرحبا يا صديق، انا احتاج الى متابعتك وتصويتك لي
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit