0

So I'm new to Ruby and I'm looking to make my code a little neater and cleaner looking. Basically I have a these methods in a class. I made a reverse polish notation calculator. Now I know in theory "don't repeat yourself" but as you can see in the plus minus divide and times methods I do. Basically the only thing that changes is a the +, -, /, *. I'm wondering what is the best way to go about fixing that? To start, here's the code. Some of my thoughts are below.

class RPNCalculator

def initialize
    @calculator =  [ ] 
end

def push(num)
    @calculator << num
end

def plus
    value = @calculator[-2].to_f + @calculator[-1]
    @calculator.delete_at(-2)
    @calculator.delete_at(-1)
    @calculator << value
end

def minus
    value = @calculator[-2].to_f - @calculator[-1]
    @calculator.delete_at(-2)
    @calculator.delete_at(-1)
    @calculator << value
end

def divide
    value = @calculator[-2].to_f / @calculator[-1]
    @calculator.delete_at(-2)
    @calculator.delete_at(-1)
    @calculator << value
end

 def times
    value = @calculator[-2].to_f * @calculator[-1]
    @calculator.delete_at(-2)
    @calculator.delete_at(-1)
    @calculator << value
end

Basically, I'm thinking to make another method end_calc() which takes a block(?). I'm wondering what that would look like. For example this is what I was thinking about.

def end_calc(&block)
value = @calculator[-2].to_f &block @calculator[-1]
    @calculator.delete_at(-2)
    @calculator.delete_at(-1)
    @calculator << value
end

def plus
  end_calc(+)
end

Basically in the methods plus minus etc, I just call end_calc plus the necessary + - / * sign. How can I write it syntactically so that the + method is inserted into end_calc? Or what should I go about studying in order to understand how to do that? Is there another what to do it that may be more effective that I do not know about yet?

1 Answer 1

0

You can use following code:

class RPNCalculator
  def initialize
    @calculator = []
  end

  def push(num)
    @calculator << num
  end

  def plus
    do_operation(:+)
  end

  def minus
    do_operation(:-)
  end

  def divide
    do_operation(:/)
  end

  def times
    do_operation(:*)
  end

  private

  def do_operation(operation)
    operand_2, operand_1 = @calculator.pop, @calculator.pop
    value = operand_1.to_f.send(operation, operand_2)
    @calculator << value
  end
end

Also, I think you should rename times to multiply, because method times is often used to repeat block of code several times and it may cause misunderstanding.

3
  • I used some Ruby magic here, can explain it if it isn't clear enough.
    – hedgesky
    Jul 8, 2015 at 22:40
  • 1
    operand_1, operand_2 = @calculator.pop(2) :)
    – cremno
    Jul 9, 2015 at 2:14
  • @cremno nice addition)
    – hedgesky
    Jul 9, 2015 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.