Skip to content

Ruby Hello World

Last Updated on 14/11/2020

Ruby Logo


This guide will show you how to write a Ruby Hello World and get a basic and initial understanding about this programming language.

Why Ruby?

As a Java developer for four years long, I’ve always had the curiosity to learn a new object-oriented programming language. Given a great feedback I had from a job interview I went a few weeks ago, and some research on the international software development scenario, I’ve found out that mastering just one language may not be enough.

Specialists are well recognized at their fields of work on software development (and I seriously intend to become one on Java), but the business dynamism grows it’s requirements every day, consequently, requiring the most versatile professionals.

By the given experiences and observations, and following the path of lots of respected developers on the community along with some personal interest, I decided to finally put some serious time on studying a new language, then Ruby is the chosen one!

I’ve already read a lot about the language, however, I’m going to dedicate this post to a simple Ruby Hello World to not extend it so much.

Download and Install Ruby

Windows

Go to Ruby installer page and download Ruby latest version:

ruby download page

Once downloaded, run the downloaded file and install Ruby, checking the two options below:

ruby installer optional tasks

To make sure things worked as expected, open your prompt and type ruby -v, which should print the installed Ruby version:

C:\Users\Bruno>ruby -v
ruby 2.2.4p230 (2015-12-16 revision 53155) [x64-mingw32]

Ubuntu

Most of Linux distributions already have Ruby installed, but if you fail to get the correct version by running ruby -v, type the following on the console:

sudo apt-get install ruby2.2

Now, the ruby -v should print the installed version.

Ruby Hello World!

Creating a Hello World in Ruby is so simple that all you need to do is the following:
Go to your prompt and type irb to open IRB (Interactive Ruby Shell)
Type the following then press Enter:

puts "Hello World!"

Watch the magic happening:

Hello World
=> nil
irb(main):004:0>

But, let’s put a little more fun on it, by creating an actual Ruby class, instantiating and running it.

Create a file named hello_world.rb on a folder of your choice, and put the following inside that file:

# This is a class!
class HelloWorld

	# Constructor receiving 'your_name' as parameter
	def initialize your_name
	
		# Stores the parameter on an instance variable
		@your_name = your_name
	end

	# Method which prints a welcome message followed by a name
	def say_hello
		puts "Welcome to Ruby, #{@your_name}!"
	end
end

# Instantiate the class passing my name as an argument to the constructor
hello_world = HelloWorld.new "Bruno Gasparotto"

# Invokes the say_hello method, which prints the message
hello_world.say_hello

Save the file, go to your prompt and navigate to the folder where you saved it, then type the following to run it:

ruby hello_world.rb

Which should prints a message with your name:

Welcome to Ruby, Bruno Gasparotto!

That’s it for meeting Ruby! I’m really enjoying studying the language, as you can see, it’s sintax is very clean and simple, besides that, I’m finding kinda funny to code Ruby.
Also, I’m going to release new posts very soon, so keep in touch to follow the updates.

Hope it helps. Cya!

Leave a Reply

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