Executing Ruby from the Command Line

Ruby allows users to run lines of code as command-line options to the ruby tool. The ‘-e’ command line flag is used to accomplish this. Let’s run ‘Hello Ninjas! Let’s learn Ruby.’. 

Example:

ruby -e ' print "Hello Ninjas! Let’s learn Ruby.\n" '

You can also try this code with Online Ruby Compiler

Run Code

Output:

Output while executing Ruby on CL

Though this ‘-e’ flag only allows a single line of code to be executed, this does not necessarily prevent the use of multiple ‘-e’ flags on a single command line to execute multiple lines.  We can use multiple ‘-e’ flags.

Example:

ruby -e ' print "Hello Ninjas! Let’s learn Ruby.\n" ' -e ' print "Using of extra -e flag allowed printing multiple lines.\n" ' -e ' print "Hope you learned something new today!\n" '

You can also try this code with Online Ruby Compiler

Run Code

Or

ruby -e ' print "Hello Ninjas! Let’s learn Ruby.\nUsing of extra -e flag allowed printing multiple lines.\nHope you learned something new today!\n" ' 

You can also try this code with Online Ruby Compiler

Run Code


Comments

Leave a Reply

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