Writing A Ruby Program: Part I

Joy Evans
3 min readOct 5, 2021

--

Photo by Joshua Fuller on Unsplash

I remember back in my early BootCamp days, and I was learning how to work with Ruby. It felt as though every assignment involved the phrase “Hello World!” and I wanted to scream; however, somehow, when that string returned, I also shouted with glee because I solved the assignment correctly. Fast forward, I’ve since learned Ruby on Rails, JavaScript, and React, but I haven’t gone back to those old school days of just Ruby and Ruby alone.

In this tutorial, I’m going to walk you through creating a program in Ruby. But to make it more interesting, we’ll have the program ask for the user’s name. In the end, it should look like this:

#output
Hello and Welcome to Activity Generator!
The rules are simple: Enter your name when prompted and let the program randomly select your activity
Simple right?
So please enter your name.
#input
joy
#output
Hi joy, so your activity for this week is...

Getting Started

So let’s get started writing a new program and create a new file by doing the following: touch greetings.rb or the “New File” option in the VS Code Editor.

Once greeting.rb is created, we’ll begin a typing out our greeting:

puts "Hello and Welcome to Activity Generator!" +"\n" +"The rules are simple: Enter your name when prompted and let the program randomly select your activity" +"\n" +"Simple right?"

So in this code snippet, we’re using puts and \n . puts is a Ruby method used to tell the computer to print text to the screen. In addition to puts , I also used \n to create a new line.

gets.chop

So now that we have the introduction set up, we can prompt the user for their name so we can use it in a response. We’ll start by using the puts method to request the user’s name.

puts "So please enter your name."

Next, we’ll create a variable to store the input given by the user by using another method called gets. But instead of using name = gets , we’ll be using name = gets.chomp instead. The reasoning behind this is due to the creation of a new-line character when you use gets . You can check out my previous blog about using gets methods here.

So if we continue, your code should look like the following:

puts "So please enter your name."name = gets.chopputs "Hi #{name}, so your activity for this week is..."

Running a Ruby program

So now that you have your code, it’s time to run your program. We’ll start by using the ruby command and follow it up with the name of the Ruby file. It should be like follows:

ruby greetings.rb

Once it runs, you’ll see the greeting and prompt asking you the following question:

#output
Hello and Welcome to Activity Generator!
The rules are simple: Enter your name when prompted and let the program randomly select your activity
Simple right?
So please enter your name.
#input
joy
#output
Hi joy, so your activity for this week is...

Now that you’ve learned how to create a file, get input, and run a program, you can continue adding to your program. Feel free and comment and share. Thanks for reading!

--

--

Joy Evans

Software Engineer | C# | Angular | Ruby on Rails | Javascript | React