Build a Dice Validator in Ruby

Building validations in programs is a common requirement. In this exercise we’ll examine how to ensure a set of dice are valid by implementing a method that returns false if the dice are outside the range of 1-6.

Summary

Build a dice validator program.

Exercise File

Code File

Exercise Description

Implement a program that checks to see if a roll of two dice is valid or not.

Example Input/Output

valid_dice? 4, 2 # => true
valid_dice? 6, 6 # => true
valid_dice? 5, 1 # => true
valid_dice? 8, 2 # => false
valid_dice? 1, 7 # => false
valid_dice? 9, 7 # => false

Real World Usage

Working with range based validations are a common requirement for a wide variety of applications. Imagine a situation where you need to see if a user’s zip code falls inside of a certain range of numbers. There are a number of ways to implement this solution, I personally opted for the version that worked with collections of data.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on