0

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

1
  • 2
    File contents: #!/usr/bin/env ruby\n p 'hi!' then run chmod 2775 ./file.txt then you can run ./file.txt and the output will be "hi!" Commented Apr 24, 2021 at 21:22

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer for Unix-like systems. I don't think hashbangs work out-of-the-box on Windows, and to be honest I don't really know how to emulate them offhand.
I updated my answer for default open method in windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.