From: James Edward Gray II Date: 2004-09-30T07:08:31+09:00 Subject: Math Tricks Quick question. When I run: #!/usr/bin/env ruby class RPNCalc def initialize(stack = [ ]) @stack = stack end def push(number) if number.kind_of? Numeric @stack.push(number) return @stack[-1] else return nil end end def add; return binary { |l, r| l + r } end def sub; return binary { |l, r| l - r } end def mul; return binary { |l, r| l * r } end def div; return binary { |l, r| l / r } end private def unary(&op) if @stack.size < 1 raise "Insufficient elements on stack for operation." end return push( op.call( @stack.pop ) ) end def binary(&op) if @stack.size < 2 raise "Insufficient elements on stack for operation." end return push( op.call( *@stack.slice!(-2, 2) ) ) end end calc = RPNCalc.new puts calc.push(3) puts calc.push(5) puts calc.add puts calc.push(2) puts calc.mul puts calc.push(2) puts calc.push(3) puts calc.add puts calc.div __END__ I see 3 as the last output when I expect to see 3.2. Why is that? James Edward Gray II P.S. I wanted to collapse those math defs to one line for easy reading and found the semicolon trick used above. Is that the only way to do it, or is there a method similar to if ... then ... end?