Rookie - Ruby: Run file in terminal
Ruby programs generally use the '.rb' extension, so in order to run a ruby file that you've written, you need to save it somewhere with that extension first- eg. 'my-app.rb'.
It's a good idea when starting out to save it in a folder inside your "Home" directory (/Users/your user name/). You can find that in the mac "Finder" by clicking on the folder on the left hand list that's named "your username". In your terminal, your home directory is shortened to '~/' - and you can easily change directory into it with that shortcut:
cd ~
While I've been learning, I've stuck to a quick, short directory to store my files- '~/code/'. Anything will do, but it's much quicker to type 'cd ~/code/my-app.rb' than to type something long like 'cd ~/Documents/Programming/Ruby/my-app.rb' every time. So when you're deciding on where to save, think about how much you'll have to type in terminal! :)
Once you've saved your file, and used 'cd' to change into the directory you've saved it in, you use the command 'ruby' to run it.
ruby my-app.rb
That's about all there is to actually running your file! There's so much more to using the terminal, and writing code- but there's plenty of info out there on how to start.
I found Chris Pine's "Learn To Program" really simple and easy to follow. There are plenty of other resources out there, too! Try out Try Ruby to get going straight in your browser.
How to run Ruby scripts in Command Line?
You can put scripts in a file with a .rb
extension and run it with:
ruby import_csv.rb
Your file import_csv.rb
would contain:
#!/usr/bin/env ruby
require "jekyll-import";
JekyllImport::Importers::CSV.run({
"file" => "my_posts.csv"
})
I think the -rubygems
switch is not needed since it is enabled by default. If not, add the line require rubygems
before the other require line.
Run a Ruby Script with Python
Most probably, ruby
in your python script is not the same as ruby
in your shell. This can happen if you e.g. have a default system ruby installed (e.g. from the system packages) and you install another ruby version via RVM.
When using shell, the RVM automatically loads it's environment from the shell init scripts (e.g. ~/.bashrc
) and knows which ruby to use from its settings. Whereas your python script does not load any rvm environment (it's not a login shell) and calls the default system ruby.
In that case, you need to explicitly call the correct ruby from the RVM in your python script. You can to it by calling the RVM wrapper:
- browse directories under
~/.rvm/wrappers/
and find the correct ruby version and gemset that you want to use - in your python script, call the
ruby
command from this wrapper directory instead of the plain `ruby, something like:
rvm_ruby = os.environ['HOME'] + "/.rvm/wrappers/ruby-2.3.0-p100@myproject/ruby"
os.system(rvm_ruby + " ./wpscan.rb -u www.mysite.com")
This should fix your problem.
How do I run a ruby script from a webpage?
In general, to run off simple commands like that, you can run ruby scripts via the terminal:
ruby /path/to/ruby/script.rb
Otherwise, if you insist on using a webserver for such purpose (or, if your use-case requires so), you should look at simple ruby-based web servers/frameworks like rack, sinatra, etc.
Particularly, in rack, you can save the following file (lets, say as: hello.rb):
require 'rubygems'
require 'rack'
def application(env)
[200, {"Content-Type" => "text/html"}, "Hello world."]
end
Rack::Handler::Mongrel.run method(:application), :Port => 9292
Then, you can run the above file using terminal:
ruby /path/to/hello.rb
And, this will create a local web server with port 9292, and you can open the above page at: http://127.0.0.1:9292/
, and the page will say: Hello world.
How do I execute a Ruby script using VSCode?
You can use Coder Runner Extension.
Once adding the extension you press Command + Shift + P
and select run by language
then choose ruby
Related Topics
How to Use 'Debugger' and 'Pry' When Developing a Gem? (Ruby)
How to Extract Specific Elements from an Array
How to Require Activerecord in Irb
Use Some Middleware Only for Specific Rack Website
How to Properly Install Bootsnap on Windows
Knowing When Resque Worker Had Completed Job
Error Pushing Ruby on Rails to Heroku
Extract Text Between Two Tags Using Regex in Ruby
How to Get the Current Working Directory's Absolute Path from Irb
Enter & Ioerror: Byte Oriented Read for Character Buffered Io
Why Sinatra Request Takes Em Thread
Routing Error During "Ruby on Rails-Tutorial"
Heroku-18: Git Push Fails. Showing Different Versions of Ruby on Push
How to Add "Access-Control-Allow-Origin" Headers to API Response in Ruby
Ruby: Unexpected End-Of-Input, Expecting Keyword_End for If Statement
Ruby Before_Validation Triggers Infinite Loop of Call Back
Jekyll Custom Theme- Gemspec Bundle Install Error: Unexpected Unary-, Expecting Keyword_Do