9. Ruby Beyond the Basics I
Activity 9.01: Invoice Generator
Solution
- Create a new Ruby file.
- Define the
invoice_generator
method and thep1
andp2
variables to be used in the program using&block
andblock_given?.
We call theinvoice_generator
method 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_generator
method has ayield
keyword, which will only execute the block of code and pass the product prices if the first block of code is passed.First,
calc_discount
is called and then a block code is executed, which, in turn, passes the customer details to thedetails
method. - 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_discount
is the method where we calculate the sum of the product prices and...