9. Ruby Beyond the Basics I
Activity 9.01: Invoice Generator
Solution
- Create a new Ruby file.
- Define the
invoice_generatormethod and thep1andp2variables to be used in the program using&blockandblock_given?.We call theinvoice_generatormethod and pass the product prices along with a block of code:def invoice_generator(p1,p2, &block) Â Â Â Â yield calc_discount(p1,p2) if block_given? end
The
invoice_generatormethod has ayieldkeyword, which will only execute the block of code and pass the product prices if the first block of code is passed.First,
calc_discountis called and then a block code is executed, which, in turn, passes the customer details to thedetailsmethod. - Next, we will define the discount calculator method,
calc_discount, which will calculate the discount on the products, add it to the product, and print the final price of the product.calc_discountis the method where we calculate the sum of the product prices and...