Ruby case when表達式實際應(yīng)用解析
Ruby語言中存在著許多表達式,這些表達式用法不盡相同,實現(xiàn)的功能也不同。熟練的掌握這些表達式的用法,可以有助于我們編程的方便性。今天看到有人在Groovy的郵件列表上問Groovy能不能支持Ruby case when表達式: #t#
Ruby case when表達式代碼示例:
- car = "Patriot"
- manufacturer = case car
- when "Focus": "Ford"
- when "Navigator": "Lincoln"
- when "Camry": "Toyota"
- when "Civic": "Honda"
- when "Patriot": "Jeep"
- when "Jetta": "VW"
- when "Ceyene": "Porsche"
- when "Outback": "Subaru"
- when "520i": "BMW"
- when "Tundra": "Nissan"
- else "Unknown"
- end
- puts "The " + car + " is made by
" + manufacturer - car = "Patriot"
- manufacturer = case car
- when "Focus": "Ford"
- when "Navigator": "Lincoln"
- when "Camry": "Toyota"
- when "Civic": "Honda"
- when "Patriot": "Jeep"
- when "Jetta": "VW"
- when "Ceyene": "Porsche"
- when "Outback": "Subaru"
- when "520i": "BMW"
- when "Tundra": "Nissan"
- else "Unknown"
- end
- puts "The " + car + " is made by
" + manufacturer
然后Guillaume給出了這么一段Ruby case when表達式代碼:
- def car = "Patriot"
- def manufacturer = match(car) {
- when "Focus", "Ford"
- when "Navigator", "Lincoln"
- when "Camry", "Toyota"
- when "Civic", "Honda"
- when "Patriot", "Jeep"
- when "Jetta", "VW"
- when "Ceyene", "Porsche"
- when "Outback", "Subaru"
- when "520i", "BMW"
- when "Tundra", "Nissan"
- otherwise "Unknown"
- }
- println "The $car is made by
$manufacturer"- def match(obj, closure) {
- closure.subject = obj
- closure.when = { value, result ->
- if (value == subject)
- throw new MatchResultException
(result: result)- }
- closure.otherwise = { return it }
- closure.resolveStrategy =
Closure.DELEGATE_FIRST- try {
- closure()
- closure.otherwise()
- } catch (MatchResultException r) {
- r.result
- }
- }
- class MatchResultException
extends RuntimeException {- def result
- }
- def car = "Patriot"
- def manufacturer = match(car) {
- when "Focus", "Ford"
- when "Navigator", "Lincoln"
- when "Camry", "Toyota"
- when "Civic", "Honda"
- when "Patriot", "Jeep"
- when "Jetta", "VW"
- when "Ceyene", "Porsche"
- when "Outback", "Subaru"
- when "520i", "BMW"
- when "Tundra", "Nissan"
- otherwise "Unknown"
- }
- println "The $car is made
by $manufacturer"- def match(obj, closure) {
- closure.subject = obj
- closure.when = { value, result ->
- if (value == subject)
- throw new MatchResultException
(result: result)- }
- closure.otherwise = { return it }
- closure.resolveStrategy =
Closure.DELEGATE_FIRST- try {
- closure()
- closure.otherwise()
- } catch (MatchResultException r) {
- r.result
- }
- }
- class MatchResultException
extends RuntimeException {- def result
- }
我不是很喜歡里面用異常來控制程序的流程,而且覺得“when "Focus", "Ford"”中間的逗號不夠直觀,因此就在上面的Ruby case when表達式代碼的基礎(chǔ)上做了一些修改:
- def match(subject, closure) {
- def whenMap = [:], otherwise = null
- closure.when = { map -> whenMap.putAll(map) }
- closure.otherwise = { otherwise = it }
- closure.resolveStrategy = Closure.DELEGATE_FIRST
- closure()
- def result = whenMap.find { condition,
value -> subject in condition }- return result ? result.value : otherwise
- }
- def manufacturer(car) {
- match(car) {
- when "Focus": "Ford"
- when "Navigator": "Lincoln"
- when "Camry": "Toyota"
- when "Civic": "Honda"
- when "Patriot": "Jeep"
- when "Jetta": "VW"
- when "Ceyene": "Porsche"
- when "Outback": "Subaru"
- when "520i": "BMW"
- when "Tundra": "Nissan"
- otherwise "Unknown"
- }
- }
- println "The Patriot is made
by ${manufacturer('Patriot')}"- println "The QQ is made by $
{manufacturer('QQ')}"- def match(subject, closure) {
- def whenMap = [:], otherwise = null
- closure.when = { map -> whenMap.putAll(map) }
- closure.otherwise = { otherwise = it }
- closure.resolveStrategy = Closure.
DELEGATE_FIRST- closure()
- def result = whenMap.find { condition,
value -> subject in condition }- return result ? result.value : otherwise
- }
- def manufacturer(car) {
- match(car) {
- when "Focus": "Ford"
- when "Navigator": "Lincoln"
- when "Camry": "Toyota"
- when "Civic": "Honda"
- when "Patriot": "Jeep"
- when "Jetta": "VW"
- when "Ceyene": "Porsche"
- when "Outback": "Subaru"
- when "520i": "BMW"
- when "Tundra": "Nissan"
- otherwise "Unknown"
- }
- }
- println "The Patriot is made
by ${manufacturer('Patriot')}"- println "The QQ is made by $
{manufacturer('QQ')}"
以上Ruby case when表達式代碼在Groovy 1.6下編譯通過。