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.

Example Input/Output

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

Building a calculator is a popular interview question and a good approach for learning a programming language. In this guide we’re going to build a Prefix Notation calculator in Ruby, which enables users to enter an operator, followed by any set of numbers to calculate.

Summary

Build a Prefix Notation Calculator in Ruby.

Exercise File

Exercise Description

Build a method that performs addition, subtraction, multiplication, and division on any number of values passed to the method. Additionally, ensure to handle improperly entered requests (bonus points if you build your own error handling class).

Real World Usage

This is a popular interview coding question since it requires developers to showcase skill when it comes to working with mathematical calculations in Ruby. Additionally, it offers a number of potential implementation options, including a wide range of options when it comes to managing improper input from users.

Solution

In many cases, lambdas are the most effective when they’re used in conjunction with methods. A method is limited to taking a single block in Ruby, however by leveraging lambdas you are able to pass in as many blocks as needed.

Summary

Implement a method that can accept multiple blocks by leveraging lambdas.

Exercise File

Exercise Description

Build a method that takes multiple lambdas as arguments in order to illustrate how a single method can be passed multiple blocks as arguments.

Real World Usage

By default, a method in Ruby is limited to only receiving a single block as an argument. This is where lambdas (also known as closures in other languages) are able to extend the functionality of a single method. A method can take an unlimited number of lambdas as argument, so therefore lambdas allow you to give plain Ruby methods the ability to accept multiple blocks as arguments. This exercise helps to showcase this behavior by building a User tracking solution.

Solution

There are some key differences between procs / lambdas and traditional methods in Ruby, however the differences can many times be subtle. In this exercise we’ll examine how variable scope is processed differently between the different options.

Summary

Build a program that illustrates the differences in variable scope in Ruby between Procs / Lambdas, and traditional methods.

Exercise File

Exercise Description

Build a traditional method and a lambda or Proc and illustrate how the two processes have different access levels for local variables.

Real World Usage

This exercise showcases one of the subtle differences between Procs / lambdas and plain Ruby methods. At a high level it may seem like the three options can be used interchangeably, however in this guide we’ll walk through how they approach variable access much differently.

Solution

In the last guide I implemented an XML parser that counted all of the lines from Shakespeare’s Macbeth play. That implementation worked, however it used an iterative approach, which resulted in a highly coupled method that would only be able to work on another XML file if it had an identical structure and set of tag names. In this guide we’re going to refactor the solution to utilize a functional approach.

Summary

Refactor the Macbeth XML parser/counter so it uses a functional programming approach.

Exercise File

Exercise Description

Split the macbeth_parser method into multiple methods that utilize functional programming calls instead of the iterative approach. The RSpec tests should still pass, which means that the resulting output should be identical to the current implementation.

Real World Usage

This solution was more challenging to build out than the iterative approach. However the result will be a more scalable implementation that can be utilized by other XML files. Anytime that you are building a program, it’s typically a good idea (and a good practice) to design with modularity in mind.

Solution

When it comes to working with APIs, two of the most common tools that you will use are JSON and XML. In this guide we’ll walk through a challenging task in which we ingest the full play of Shakespeare’s Macbeth in XML and convert it into a hash that sums the total number of lines for each character in the play.

Summary

Exercise File

Exercise Description

Implement a method that parses Shakespeare’s Macbeth play (stored in XML). The method should return a hash, with the key being the name of the character and the value being the total number of lines the character had.

Real World Usage

If you struggle with this exercise, don’t be discouraged. This is not a trivial coding exercise. There are a number of ways to implement this solution, and in fact in the next guide I’m going to show an alternative solution that refactors the solution and utilizes a functional approach. To properly build this program you will need to be able to:

Parse XML data

Convert XML data into a hash data structure

Worked with multiple nested iterators

Perform calculations on dynamic data

Solution

When building scripts, it’s a common requirement to remove and update multiple items in a collection of strings. In this guide we’re going to mimic the process needed to take a list of require statements and generate a list of file names.

Summary

Implement a method that generates a list of file names based on a collection of require statements.

Real World Usage

Imagine that you were asked to build a feature that took in the contents of a file and you had to both remove and add content for each array element. This is a common requirement for tasks such as: building file parsers, developing view helper methods, and many more features.

Solution

Working with infinity can be a unique challenge in many different programming languages. Thankfully Ruby offers a helpful library that allows you to implement infinity into programs, and that’s what this guide focuses on.

Summary

Build a method that utilizes infinity in order to generate a string based collection of any size.

Example

Real World Usage

Technically this particular coding exercise could be built by leveraging ranges, which is the common convention. However, there are times, especially when working with mathematical equations that you will discover the need to implement the concept of infinity.

Solution

Whether it’s a complex coding interview question or algorithm implementation, there are many times where you will be asked to combine a large number of processes into a single feature. In this guide we’ll examine how to build a non-trivial program that takes in an array of file names and generates a hash collection where each file type is the key and the value is an array of their respective file names.

Summary

Build a method that takes in an array of file names and filters them into an organized hash.

Solution

Parsing file types can be a complex process. However, by leveraging the grep method you can leverage regular expressions to quickly return a full list of all of the file types that you’re searching for.

Example

Real World Usage

Ruby provides developers a number of options when it comes to implementing solutions. As a dev, one of the daily tasks that you’ll have is to work through various solutions to find the one that works the best for your particular program. There were multiple ways to solve this exercise and being able to experiment with what makes one solution a better fit than another is important. Additionally, if you implemented the solution that I utilized, you will have also worked with: regular expressions and Unix command tools.