Integrate a Helper Module in Ruby

0
641

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here