5 razones para usar Whatsapp Business

Actualmente los clientes no se detienen a esperar que las empresas les enviemos nuestro brochure a su domicilio; ellos nos buscan en Internet y buscan la manera más rápida y efectiva de comunicarse…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Building CLI Application in Ruby

The CLI was the principal from of engaging with most computer systems since its inception in the 1960s. Through the use of computer terminals, CLIs are typically implemented with with a command line shell, which is a program that handles the interface by serving as the intermediary between the entity using it (the client) and the machine. Essentially, the CLI allows its user to enter commands in the form of text and convert that text into its perspective machine code to run that command.

To gain a deeper understanding of CLIs, Ruby, SQLlite, and Active Record, I spent the last week building a vanilla ruby CLI application that tests your knowledge with trivia questions. The following article will go through the ideation process, our setup, the game modes, and a major road block we hit as well as the solution.

My partner and I were stumped for ideas and spent our first day trying to figure out what to do. All we knew is that we wanted to seed our data from an API, and preferably make a game of some sort. After brainstorming and pitching ideas to peers, which was often met with constructive rejection, we finally settled on a questions based trivia game.

Because we are persisting data using SQLite, our environment file has to establish and connect to our database. From here we want to make sure all of our files are loaded in one spot.

Since we want to persist the data in our game we first have to flesh out our models. The app will have three models: Player, Question, and QuestionMaster. There is a many-to-many relationship between Players and Questions, and the QuestionMaster model serves as our join table to complete the many-to-many through relationship. The QuestionMaster knows which questions were asked of a player and whether or not they got it right or wrong. Understanding our schema is an important in the setup process.

Using active record, we created our migrations and produced our schema. With the database set up, we create our classes that inherit instance methods from Active Record and explicitly define their relationships. Leveraging the power of Active Record will make our lives much easier.

In our app, we implemented different classes for the the different modes of the game. Each mode queries the api differently based on the mode’s parameters. Since the validation process is the same across modes, I will walk though just the Classic Mode.

In Classic Mode the player is asked 10 questions of their choice of category and difficulty. They have three opportunities to get a question wrong. If they get the question wrong, they loose 1 health and continue to the next question. If they loose all of their health before the end of the 10 questions, they lose and the game ends. Show classic method pic

One of the biggest road blocks was implementing our question validation feature. A feature of the game is that once you’ve been asked a question, you will never get asked that question again. The feature keeps the game fresh and keeps the player on their toes. The main reason why we persisted questions was to make sure we can have this feature, since the Open Trivia Database API does not id their questions and results are random.

Lastly, every time we ask a player a question, we immediately persist it into our database, enabling us to keep a record of the question we asked the player for future validations.

This project allowed me a greater perspective into programming in Ruby, interacting with SQL databases and ORM frameworks, and using the terminal. Tangentially, I got to flex some game design muscles, exercising different techniques and practices. Our solution to our road blocks shows how robust Ruby can be when paired with an equally robust ORM. I am sure we could refactor this even more, but that is an article for my future self.

Add a comment

Related posts:

The Role of Stablecoins in a Decentralized Ecosystem

Banks. As much as people may hate them, everyone to some degree needs them. However, not everyone has access to a bank account around the world or a safe place to store their hard-earned money. Even…

Lifestyle Changes to Prevent Lines and Wrinkles ?

Certainly! Here are some lifestyle changes that can help prevent lines and wrinkles:. “Lifestyle Changes to Prevent Lines and Wrinkles ?” is published by Brian Campbell.