Ruby语言中存在着许多表达式,这些表达式用法不尽相同,实现的功能也不同。熟练的掌握这些表达式的用法,可以有助于我们编程的方便性。今天看到有人在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
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
我不是很喜欢里面用异常来控制程序的流程,而且觉得“when "Focus", "Ford"”中间的逗号不够直观,因此就在上面的Ruby case when表达式代码的基础上做了一些修改:
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')}"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
以上Ruby case when表达式代码在Groovy 1.6下编译通过。