mmohitcc
mmohitcc

Reputation: 31

Executable file in ruby

any way to create an 'executable' file in ruby so you can run file.rb with ./file

I know you can just run ruby files ruby filename.rb

I also want to be able to send arguments so ./file argument_1 argument_2

Upvotes: 0

Views: 450

Answers (1)

Siwei
Siwei

Reputation: 21569

run an ordinate ruby file

  1. Add #! /usr/bin/env ruby to your first line of file.rb file.
  2. giving execute permission to this file: $ chmod +x file.rb
  3. run: $ ./file.rb

run ruby file with parameter from commandline:

use ARGV.

#! /usr/bin/env ruby
puts " parameter0 is: #{ARGV[0]}"
puts " parameter1 is: #{ARGV[1]}"

output:

$ ./test.rb a b
 parameter0 is: a
 parameter1 is: b

doubli click to run .rb file in windows

  1. install ruby in windows
  2. mouse right click .rb file
  3. open with ...
  4. choose ruby program. as the "default program"

for more details, refer to: https://techforluddites.com/windows-10-change-the-default-programs-for-opening-files/

Upvotes: 1

Related Questions