test_controller.rb
来自「ruby on rails web敏捷开发之路第二版 源代码」· RB 代码 · 共 61 行
RB
61 行
#---# Excerpted from "Agile Web Development with Rails, 2nd Ed."# We make no guarantees that this code is fit for any purpose. # Visit http://www.pragmaticprogrammer.com/titles/rails2 for more book information.#---class TestController < ApplicationController def rdoc @value = 123 end def date_dump end def calculate if request.post? @errors = [] arg1 = convert_float(:arg1) arg2 = convert_float(:arg2) op = convert_operator(:operator) if @errors.empty? begin @result = op.call(arg1, arg2) rescue Exception => err @result = err.message end end end end private def convert_float(name) if params[name].blank? @errors << "#{name} missing" else begin Float(params[name]) rescue Exception => err @errors << "#{name}: #{err.message}" nil end end 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} else @errors << "Missing or invalid operator" nil end endend
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?