CronDose | Daily Dev Guides

Daily Snippets for the Developer Life

A Better Way to Query Hash Values in Ruby

Ruby offers a wide variety of solutions for many tasks. In this guide we’re going to explore a more expressive process for querying hashes by leveraging the fetch method. Summary Integrate a hash query method that throws an error when asked to query a key that doesn’t exist. Exercise File Code File Exercise Description The standard hash querying syntax simply returns a nil value when passed a key that doesn’t exist. However this can be confusing because this is the identical behavior to what happens when you query a legitimate key that simply has a […]

Using SimpleDelegator in Ruby to Implement the Decorator Pattern

There are a number of ways to implement the decorator design pattern in Ruby. In this guide we’re going to examine how we can leverage the SimpleDelegator tool in Ruby to add additional functionality to a class. Summary Build a delegator for a Ruby class by using Ruby’s SimpleDelegator. Exercise File Code File Exercise Description Implement a delegator for a Ruby class using Ruby’s SimpleDelegator process to add functionality to a class without having to add code to the class itself. Examples # A standard Ruby class that has a name initializer attribute invoice = […]

Utilizing the Phone Gem to Create, Format, and Query Phone Numbers

Working with phone numbers, especially for International applications, can be a challenging task. In this guide we’re going to walk through how to implement the popular Phone RubyGem in order to create, format, and work with phone numbers in a Ruby program. Summary Implement the Phone RubyGem in order to work with phone numbers. Exercise File Code File Exercise Description Work with the Phone RubyGem code library in order to build four methods. The methods should be able to properly: Build a phone number object. Query a phone number for separate components, such as area […]

Use attr_extras Gem to Refactor a Ruby Class

This guide examines how to streamline a class definition in Ruby by using the attr_extras Ruby gem. This gem allows you to remove unnecessary boilerplate code when you define attributes in Ruby. Summary Refactor a Ruby class so that it uses the attr_extras gem. Exercise File Code File Exercise Description Clean up how Ruby defines its list of attributes by leveraging the attr_extras gem. This will allow you to remove unnecessary boilerplate code when listing attributes. Class to Refactor class PurchaseOrder attr_accessor :client, :total def initialize(client, total) @client = client @total = total end def […]

Build a Progress Tracking Class in Ruby

This guide walks through how to implement a progress tracking class in Ruby that can update a user’s progress and dynamically update the overall percent completed. Summary Implement a class that tracks a user’s completion percentage and dynamically allows a user to update it. Exercise File Code File Exercise Description Build a class that tracks the completed percentage of a course for a student, and when the student marks a task complete, the class should update the percent complete to reflect the updated value. Example Workflow c = Completion.new(number_of_guides: 100, total_completed: 55) c.completed_percentage # => […]

Integrate a Helper Module in Ruby

This guide walks through how to integrate Ruby modules in order to add custom helper methods to a class. Summary Build a module with helper methods and call the methods from a class. Exercise File Code File Exercise Description Build a helper module called InvoiceHelper that contains a method called taxable? that checks the source of an order and returns if it’s taxable order or not. Then build a class called Sale that has a total and source attribute. Then build a method called place_order that returns the total plus a 6% tax rate if […]

Duplicating Objects in Ruby by Leveraging Symbols

In this guide you’ll walk through how to leverage symbols in Ruby in order to duplicate objects, including how they can share an object_id. Summary Implement a method that clones a string so that they use the same object_id. Exercise File Code File Exercise Description Given an array of strings, normally each string would have its own object_id. In this exercise you’ll need to figure out how to convert each element so that they share the same object_id (assuming that the strings are equal). Example Input str_array = [‘some word’, ‘some word’, ‘some word’] cloned_array […]

Implement Range Finder Method in Ruby

This guide examines how to find the range of values in an integer based array by returning an array that contains the high and low values. Summary Implement a method that indicates the range of values contained in an array of integers. Exercise File Code File Exercise Description Given an array of integers, build a method that returns an array that contains two elements, the low and high values of the array. This will indicate the full range of values contained in the array. Example Input To return the count by itself: [5, 1, 3, […]

Building a Combination Lock Picker in Ruby

In this guide you’ll learn how to build a method that returns the potential number of combinations associated with building a lock picking method, including both how to count the number of permutations and generating the full set of options. Summary Build a method that helps give the full set of combinations associated with figuring out the numbers to a combination lock. Exercise File Code File Exercise Description Given an array of integers, build a method that returns either the count or the actual combinations of numbers it will take to pick a combination lock. […]

Find Graph Weights for a Mapping Application in Ruby

This guide walks through how to work with a mapping application and apply graph traversal techniques to find the distance for each weight in a graph. Summary Build a method that finds the distance for each weight in a directed graph. Exercise File Code File Exercise Description Given a route on map, that contains stops that have a weight measured between each stop, and the total distance, figure out the value that each weight represents. Example Route Example Data Given the total distance of: 15 And the three nodes, represented by an array: [[1, 3], […]

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on