Writing A Ruby Program: Part II

Joy Evans
3 min readOct 11, 2021
Photo by Joshua Fuller on Unsplash

So last week, I can up with this fun idea to develop a “Random Activity Generator” using Ruby. We reviewed how to create a file, get input, and run a program. This week, I‘ll go over selecting random values, case statements and using require_relative. So let’s get started:

Using rand with Inclusive Values

So far, we’ve prompted our program to ask for a name. It then prints out a response, like so: Hi Joy, so your activity for this week is... . Now it’s time to create a variable named activity . This variable will select a random integer ranging from 1 to 10. It’s important to note that we have to work with an inclusive range because we don’t want to exclude the final value of 10. So this variable should be as follows:

activity = rand(1..10)

Remember: Two dots (inclusive range) includes the final value and three dots (exclusive range) excludes the final value.

Case Statement

Next, we’ll give it a list of activities to be selected based on the chosen value. Since I need the program to select from ten different activities, it’ll be best to use a case statement because it helps your code become more readable and cleaner. Once we include our activity variable to the case statement, it should be as follows:

case activitywhen 1puts "Have a movie night"when 2puts "Try a new activity you've never done before"when 3puts "Try a new sport"when 4puts "Learn a new skill"when 5puts "Learn the basics of a new language"when 6puts "Learn mindfulness or meditaion"when 7puts "Become a better cook"when 8puts "Find a new hooby"when 9puts "Journal"elseputs "Treat yourself to a manicure, pedicure, etc."end

require_relative

Like I mentioned earlier, towards the end of the case statements section, I want my code to become more readable and cleaner. Once I add the variable to select a random integer and the case statement to the greetings.rb file, it becomes too much to read. The best way to solve this would be to move this code to another Ruby file. We’ll name this new file activity.rb , but the next step is finding a way to access this new file.

That’s when the require_relativecomes into the picture. The method references and executes code written in another file within your directory. The syntax is as follows:

require_relative 'filename'

After typing require_relative toward the bottom of greetings.rb , you follow it up with the name of the file you’re looking to reference in strings. In addition to your previous code from ‘Writing A Ruby Program: Part I”, the greetings.rb file should look like this:

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?"puts "So please enter your name."name = gets.chopputs "Hi #{name}, so your activity for this week is..."
require_relative "activity"

And once you run your program, your output should look similar to this:

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.// joyHi joy, so your activity for this week is...
Learn a new skill

To recap, you’ve learned how to use rand with inclusive values, case statements, and reference code from another file. Feel free to comment and share, and thanks for reading!

--

--

Joy Evans

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