DEV Community 👩‍💻👨‍💻

Gabor Szabo
Gabor Szabo

Posted on

Hello World++ in Ruby

We start with the required "Hello World!" example.

Hello World! in double quotes

Create a file called hello.rb with the following content.

puts "Hello World!"
Enter fullscreen mode Exit fullscreen mode

Then go to the command line (terminal), to the same directory where you have the file hello.rb saved and type ruby hello.rb. It will print "Hello World!" to your screen.

What you can see here is that if you would like to have some text in your program you need to put them between quotes (") and you can use the puts function to print something to the screen. Any text between quotes is called a "string".

Single quotes

Just to clarify, unlike in some other programming languages, in Ruby you can use either double-quotes or single-quotes to mark the beginning and the end of a string, but of course it has to be the same on both sides.

puts 'Hello World!'
Enter fullscreen mode Exit fullscreen mode

Create variable and print content using puts

The next step after greeting the world is greeting the programmer. However, before we get there we would like to create a variable. We created a variable called person and put the name of the programmer in it. We also say we assigned a value to a variable. For whatever reason most of the programmer are called Foo so we'll go with that name here too.

We then use puts to print out the content of the variable.

This will print Foo to the screen.

person = "Foo"
puts person
Enter fullscreen mode Exit fullscreen mode

Hello Foo using puts

It is not enough to print the name of the programmer, we would also want to greet the programmer.

We print the first part of the message, the variable, and the last part of the message in a single call to puts. We need to separate these parts with a comma (,).

person = "Foo"

puts "Hello ", person, ", how are you?"
Enter fullscreen mode Exit fullscreen mode

We got everything on the screen, but we are slightly disappointed as puts put each part of our message on a separate line. That's not really nice.

Hello 
foo
, how are you?
Enter fullscreen mode Exit fullscreen mode

In more technical terms, puts printed a newline after printing each one of its parameters.

Hello Foo - print

One way to improve the situation is to use the print function instead of the puts function to print to the screen.

That gave us a much nicer result as it did not print a newline after each item thereby moving the next one to the next line. However, it also did not print a newline at the end of the whole print operation, which would have been also less convenient. So in order to make it nicer we had to include \n at the end of our message to make it nicer. \n represent a newline so when we include \n in whatever we print the next thing will be printed on the following line.

person = 'Foo'

print "Hello ", person, ", how are you?\n"
Enter fullscreen mode Exit fullscreen mode
Hello Foo, how are you?
Enter fullscreen mode Exit fullscreen mode

Ruby variable interpolation

To further improve the situation we are going to use the interpolation capabilities of Ruby. We can insert a hashmark (#) followed by a variable between a pair of curly braces in a string. Ruby will replace that whole construct with the value of the variable.

person = "Foo"

puts "In double quotes #{person}"
puts 'In single quotes #{person}'
Enter fullscreen mode Exit fullscreen mode

Here however the behavior of double-quote and single-quote is different. The interpolation only works in double-quoted strings. Single-quoted strings will just include the hash-mark, the opening curly, the variable name and the closing curly.

In double quotes Foo
In single quotes #{person}
Enter fullscreen mode Exit fullscreen mode

Hello Foo - puts with interpolation

Putting the interpolation to use, here is how we greet our programmer using puts and a variable interpolated in a double-quoted string.

person = 'Foo'

puts "Hello #{person}, how are you?"
Enter fullscreen mode Exit fullscreen mode

The output looks good:

Hello foo, how are you?
Enter fullscreen mode Exit fullscreen mode

Hello Foo STDIN read

What if the person is not called Foo? What if we would like to ask the users of their name and greet them that way? We can do this by reading from the keyboard which is, in technical terms called Standard Input or STDIN.

This is how we do it:

print "Type in your name: "
person = STDIN.gets

puts "Hello #{person}, how are you?"
Enter fullscreen mode Exit fullscreen mode

Here is what happens when I run the program:

ruby examples/intro/hello_foo_stdin.rb
Enter fullscreen mode Exit fullscreen mode

It prints:

Type in your name:
Enter fullscreen mode Exit fullscreen mode

Then I type in Bar (another well known name in the programmer community) and press ENTER and this is the output I see:

Hello Bar
, how are you?
Enter fullscreen mode Exit fullscreen mode

Not nice. Why is it printed in two rows?

Because after we typed in Bar we pressed ENTER to tell the computer that we are done. However that ENTER generates a newline and that is included in the text our program receives. So in the variable person we have a newline at the end and when we print it, the next thing will be shown on a new line.

Hello Foo STDIN and strip

The solution is to remove the newline from the end of the string. We can do this using the strip method:

print "Type in your name: "
person = STDIN.gets.strip

puts "Hello #{person}, how are you?"
Enter fullscreen mode Exit fullscreen mode

Now if we run the program and identify as Bar, the output will look the way we wanted:

Hello Bar, how are you?
Enter fullscreen mode Exit fullscreen mode

Hello Foo ARGV

Finally we could also expect the users to supply their name when they run the program. For this we'll use the ARGV array that contains all the values that were passed to the program on the command line.

We'll take the first element of the array (that has index 0) and use that:

person = ARGV[0]

puts "Hello #{person}, how are you?"
Enter fullscreen mode Exit fullscreen mode

Run like this:

ruby examples/intro/hello_foo_argv.rb Bar
Enter fullscreen mode Exit fullscreen mode

This is the output:

Hello Bar, how are you?
Enter fullscreen mode Exit fullscreen mode

End

That's the end of the first entry of my Ruby series. I hope it was a good start and you'll follow me to get notified as I publish new parts.

Top comments (0)

All DEV content is created by the community!

Hey, if you're landing here for the first time, you should know that this website is a global community of folks who blog about their experiences to help folks like you out.

Sign up now if you're curious. It's free!