Introduction
Over the past few weeks, I have had the pleasure of diving into the world of Ruby and CLI Applications in Ruby. All my previous efforts in studying and working with Ruby have allowed me to create my first ever CLI Application.
Everyone who begins learning software development wants to create applications that are useful and can benefit the average person. This is why when I was tasked with creating a CLI Application for my Phase 3 Project, I decided to create an application that I and my friends would benefit from. The result? A CLI Application in which users can easily find meanings of words as well as their synonyms. A dictionary helper, if you will.
Requirements
The requirements for my Phase-3 Project were:
- Provide a CLI
- Your CLI must provide access to data from a web page
- The data provided must go at least one level deep
These requirements are quite versatile and I appreciated the fact that I could make a wide variety of different applications with these requirements. My first step was to come up with an idea that would check all the requirements!
My Idea
In the last lesson of Phase 3, my teacher provided us with an excellent example of a CLI Application: A Pokedex App. I drew inspiration from his app in which he utilised a Pokedex API to allow users to search for Pokemons and catch them. I decided to create a similar app but one which I would use frequently: a dictionary helper! My app draws data from a dictionary API and allows users to enter words they would like the definitions of as well as enter words they would like the synonym of.
Writing my CLI Application
The heart of my project was the run.rb file, in which I wrote the Ruby code to run my CLI application. There I defined various classes and methods to ensure my application ran smoothly and was user friendly.
Here's a glimpse into the Menu method from which users are able to select the option they want to explore:
def menu
puts "Menu:".blue.bold
puts "#{".dictionary".green.bold}\t - to search for a word's definition"
puts "#{".synonym".green.bold}\t - to explore synonyms of words"
puts "#{".exit".green.bold}\t\t - to exit out of Dictionary Helper"
puts "Please enter your choice from the options above:".red
gets.chomp
end
The main menu serves as a guide to users - presenting them with options and allowing them to interact with my application. I tried to keep my prompts short and understandable, so there would be no chance that users would get confused when running my application.
As seen above, I utilised the colorize gem to colour various texts when users run my application. I did this in an attempt to make my application more visually stimulating and to make it easier for users to read the prompts.
Building Functionality
One challenge was handling errors to ensure my application ran smoothly and so that users can understand the issue if they encounter one. For example, if they enter a word that is not in the dictionary API.
if !word_data.empty?
meanings = word_data[:meanings]
meanings.each_with_index do |meaning, index|
part_of_speech = meaning["partOfSpeech"]
definitions = meaning["definitions"]
puts "Meaning #{index + 1}: #{part_of_speech}".blue.bold
definitions.each_with_index do |definition, def_index|
puts "Definition #{def_index + 1}: #{definition["definition"]}".blue
end
end
elsif word_data.empty?
puts "Word not found.".blue
end
As you can see above, if a user enters a word that is not in the dictionary API I drew from, my application will return "Word not found." This was my attempt to prompt users to enter valid words instead of joking around and entering gibberish.
Conclusion
In conclusion, creating this application was a very fun and very rewarding task. I look forward to honing in on my skills more and creating more fun yet useful applications.
Top comments (1)
Great