Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024
Easy

Simple Ruby Examples to Learn Ruby

Author Sneha Mallik
0 upvote
gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Ruby is a general-purpose, high-level, interpreted programming language. It is also considered a server-side scripting language that is used in both the front-end as ruby codes can be embedded in HTML and the back-end. Ruby is simple to learn for a new developer. Ruby's characteristics include robustness, open-source, fast processing, flexibility, and consistency. Its primary applications are static websites, web servers, web scraping, data processing, and desktop applications.

Simple Ruby Examples to Learn Ruby

In this article, we will be going through some simple ruby examples to learn Ruby.

Basic Ruby Example

Ruby is not only a flexible scripting language in terms of syntax, but it is also extremely flexible in terms of how scripts can be executed. In this section, we'll look at some simple Ruby examples to learn Ruby before diving into the various ways Ruby code can be executed.

The most basic example we come across in the programming world is to print out ‘HELLO WORLD’. So, here also, we will be covering the simple ruby examples to learn Ruby to display a print output of ‘Hello Ninjas! Please look forward to learn simple Ruby examples.’.

Coding ninjas

Example:

print "Hello Ninjas! Please look forward to learn simple Ruby examples."

Output:

Output of Ruby Example

Here, we only required one line to print the output, whereas the most basic tasks require a significant amount of code structure in programming languages like C++, Java, etc.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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" '

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" '

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" ' 

Output:

Output while executing Ruby on CL

Output while executing Ruby on CL

Interactive Execution of Ruby

Ruby is an interpreted programming language. This basically means that Ruby source code is not pre-compiled like it is with languages like C or C++, but rather is compiled and executed at run time. One advantage of being an interpreted language is that we can type Ruby code directly into the interpreter and have it run interactively and in real-time. This is an excellent way to learn Ruby and experiment with different code structures.

The 'irb' tool is used to enter interactive Ruby code. We already have 'irb' installed if we are running Windows and have installed Ruby using the one-click installer. If we're using Linux, it's possible that 'irb' isn't yet installed.

We will check the installation by doing the following:

irb -v

If the ‘irb’ tool is not installed, then install it using the ‘sudo apt-get install irb’ command. After installing irb, run it as follows:

$ irb

It will redirect to the following:

irb(main):001:0> 

We will then enter the codes of Ruby to run from here. For printing a simple output from the terminal, we will use the following command:

Output of simple Ruby Example on terminal

We can also perform arithmetic operations directly from the terminal. When we press the Enter key, whatever we type at the ‘irb’ prompt is executed. 

 Arithmetic operations of Ruby

We will then look at an example where we will assign variables:

Assigning Variables in ruby

Example for taking input from the user:

Taking input from user and output

The function 'gets.chomp' is used to accept user input.

To print without a new line, use 'print' instead of 'puts'.

The function 'gets.chomp.to i' is used to obtain integer input from the user.

The function 'gets.chomp.to_f' is used to obtain float (decimal) input from the user.

These are the most fundamental concepts of simple ruby examples to learn Ruby that are crucial for new Ruby programmers.

[NOTE: Use ‘CTRL+D’ to exit from the ‘irb’ prompt.]

Executing Ruby File from Terminal

We will be creating a ruby file in VSCode with the name ‘SimpleExample.rb’.

SimpleExample.rb:

print "Hello Ninjas! Please look forward to learn more.\n"
print "I hope you're enjoying it!\n"

We will now execute the ruby file from the terminal using the command “ruby <filename>”.

Ruby file execution on terminal

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"

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!

Frequently Asked Questions

What is Ruby?

Ruby is a high-level, interpreted, and general-purpose programming language. It is mainly used for static websites, web servers, web scraping, data processing, and desktop applications. 

What are other object-oriented programming languages other than Ruby?

Other than Ruby, the most popular object-oriented programming languages are Java, C#, C++, Python, Php, JavaScript, Perl, Swift, Dart, etc.  

What are all the editor which provides support for ruby?

These are some of the editors that provide support for Ruby- Emacs or XEmacs, Vim, Jedit, Nedit, etc.

What are the advantages of learning Ruby?

The Ruby programming language is a versatile general-purpose language that can be used for a variety of tasks. Ruby is an excellent programming language for creating desktop applications, static websites, data processing services, and even for the automation tools. It's used for web servers, DevOps, crawling, web scraping, and so on.

Why is the ‘chmod 755’ command used?

An important command that grants read-write and executes permission to a particular user, group, or other users.

Conclusion

In this blog, we learned about the simple ruby examples to learn Ruby efficiently. We also discussed how to execute ruby files from the terminal, and how to use the ‘irb’ prompt to use ruby commands in the terminal directly.  We also learned how to create self-contained ruby. Install Ruby from here.

Check out here for similar ruby blogs:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, JavaScript, System Design, DBMS, Java, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 🥷✨

Thank you

Happy Learning, Ninja!

Previous article
A Sudoku Solver in Ruby 17
Next article
Comments in Ruby
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass