How to 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.

Sign Up to Start Your Own Code Camp

large

View Video

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 the source is AZ or online, and returns the total by itself if the source is any other value.

Example Input

s = Sale.new(500, 'AZ')
s.place_order # => 530.0

s2 = Sale.new(500, 'TX')
s2.place_order # => 500.0

Real World Usage

Building helper modules is an important task when it comes to code organization. This example is a practical case study on how you can add a boolean helper method to a class without having to clutter the class code.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

Jordan Hudgens

I've been a software engineer for the past decade and have traveled the world building applications and training individuals on a wide variety of topics.


View All Posts