Hi Everyone!
So I recently had to write a script in Ruby at my job to scrape a webpage for information when I realized… Wait how do you run a ruby script? I was so use to using Rails Server command to run my programs in Rails and had no idea how to do this in a non rails/ Sinatra setting. So to do this of course after you have made your script with a “touch ‘your script name'”.
When you want to run you Ruby script you’d enter the command
ruby -r "./your_script_name.rb" -e "YourClassName.your_method_name 'any parameters in your method'"
So what is going on here when you run the code is your telling the terminal to use ruby then the “-r” tell the terminal to run this program that is followed by the file name which needs the correct location in your computer; in this case the file and location would be:
"./your_script_name.rb"
The “-e” tells the program to go into the the ruby file then run the class method in this case the class would be
"YourClassName"
And the method name would be (with any addional params. If the method has no params just don't include anything after the method call)
your_method_name 'any parameters in your method'"
I hope this would clear up the mystery in running your own simple scripts.