Creating Self-Contained Ruby

Putting Ruby code in a file is obviously far more convenient and practical than using multiple -e command-line options. However, suppose we want to go a step further and be able to run a Ruby-based program by typing the file name containing the code rather than the ruby command.

Coding ninjas

On Linux or UNIX, this is accomplished by including a special line at the top of the script file that instructs the environment responsible for executing the ruby program(such as a Linux command shell) to look for the Ruby interpreter. This special line, commonly known as the ‘shebang’, consists of a ‘#’, a ‘!’, and the path to the ruby executable.

To find Ruby path

We will modify the ruby file ‘SimpleExample.rb’ accordingly:

#!/usr/bin/ruby
puts "Hello Ninjas! Please look forward to learn more.\n"
puts "I hope you're enjoying it!\n"

You can also try this code with Online Ruby Compiler

Run Code

Giving permissions for file
Output generation

We have covered the most simple Ruby examples to learn Ruby in an easier way. Hopefully, you learned alongside us and have a clear understanding!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *