calculator_controller.rb

来自「RubyonRailsC.zip ruby on rails的源码」· RB 代码 · 共 30 行

RB
30
字号
class CalculatorController < ApplicationController
	before_filter :set_charset 
	
	def set_charset 
		@headers["Content-Type"] = "text/html; charset=GB2312" 
	end 
	
	def calculate
		if request.post?
			arg1 = convert_float(:arg1)
			arg2 = convert_float(:arg2)
			op = convert_operator(:operator)
			@result = op.call(arg1, arg2)
		end
	end

	private
	def convert_float(name)
		Float(params[name])
	end

	def convert_operator(name)
		case params[name]
			when "+" then proc {|a,b| a+b}
			when "-" then proc {|a,b| a-b}
			when "*" then proc {|a,b| a*b}
			when "/" then proc {|a,b| a/b}
		end
	end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?