I don’t have anyone to ask / find out / get a hint, so I’m writing here ... There are such attempts to write a specific case, in ruby and MVC I’m completely green, so I ask you to treat this kind of code with understanding.

The task is that the object of the Handler class must contain ONLY public members add_handler, calculate, print_out . It must implement the behavior:

controller = HandlerController.new controller.add_handler(empty_handler) controller.add_handler(min_handler) controller.add_handler(max_handler) 

When the calculator method is called with one argument, the calculation is performed on the transferred array in all handler objects transferred to the controller.

 controller.calculator([1,2,3,4,5]) controller.print_out #Handler min return: 1 #Handler max return: 5 #Handler 'empty name' return "method is empty" 

When calling a method with two arguments (the second argument is an array of object names), we calculate only by the objects whose name is passed.

 controller.calculator([1,2,3],['min','max']) controller.print_out 
 class Handler attr_reader :name def initialize(name='empty name') @name = name end def calculator(array) 'Calculate method is empty' end end class HandlerController attr_reader :arr_operands def initialize @arr_operands = Array.new end def add_handler(operand) @arr_operands.push operand end def calculator(array) @arr_operands.each { |operand| puts operand } end def print_out # примерно так, наверное... puts "Handler" + arr_operands[name] + "return:" + arr_result[result] end end class MinHandler < Handler end class MaxHandler < Handler end class AverageHandler < Handler end class PremaxHandler < Handler end class SumHandler < Handler end empty_handler = Handler.new('max') max_handler = MaxHandler.new min_handler = MinHandler.new p empty_handler controller = HandlerController.new #p controller controller.add_handler(empty_handler) #p controller puts controller.arr_operands.name #puts empty_handler.name #puts controller.calculate([1,2,3]) 

Poke me into errors (or potential errors) and tell me how to implement correctly, please. It would be possible to see the implementation in other languages. Javascript for example.

  • 1. Confusion with calculate . Sometimes it is a calculator . - anoam
  • 2. Can you print_out bit more about the calculate and print_out ? The first one considers and stores the result in the instance, the second one displays the result. Right? - anoam
  • Yes, that's right, you understand) by the way, I sketched a variant of the print_out method and corrected the names of calculate. I wrote the methods that compute the whole thing; I just need to write this “interface” correctly. - Denis Oleshchenko
  • such a Task, so yes, to sort it out - Denis Oleshchenko

1 answer 1

By the way, HandlerController implements the Builder pattern.

In order. 1. I highly recommend that you familiarize yourself with OS style guides . There are things that openly hurt the eyes (for example, such name='empty name' (without spaces))

  1. def initialize (name = 'empty name')

Do not do it this way. Better to define the constant DEFAULT_NAME.

  1. attr_reader: arr_operands

If you do not need it to look somewhere outside (for example, from the outside you will receive operands), then it is better to remove this reader altogether. Inside the class, access it via @arr_operands . Instance - variables work faster than getters.

  1. @ arr_operands.each {| operand | puts operand}

I do not understand how this should work. In fact, you are trying to deduce your Handler class objects. Apparently, you need at least operand.calculate . But in general, you will most likely just need to get the result here (not sure). Then it will be more correct to call @arr_operands.map { |handler| handler.calculate(array) } @arr_operands.map { |handler| handler.calculate(array) } .

  1. def calculate (array)

You must have a second argument on the task. It would be more correct for you to define a nested class that will filter and allow iteration over an array. Inside it, you will handle the presence or absence of the second argument.

  1. print_out This is not a very working code like. In any case, try to make classes, each of which will solve only one problem. One is for storing handlers, the second is for storing results, the third is for output, etc.

Here I sketched an example. It works like this:

 iterator = Iterator.new repo = ResultReposytory.new handler = Handler.new("Test Empty") controller = HandlerController.new(iterator, repo, Printer) controller.add_handler(handler) controller.print_out # => "" controller.calculate!([1,2,3]) controller.print_out #=> "Handler Test Empty return " min_handler = MinHandler.new("minimum") controller.add_handler(min_handler) controller.print_out # => "Handler Test Empty return " "Handler minimum return 1"