Ruby case when表达式实际应用解析
创始人
2024-06-15 20:40:21
0

Ruby语言中存在着许多表达式,这些表达式用法不尽相同,实现的功能也不同。熟练的掌握这些表达式的用法,可以有助于我们编程的方便性。今天看到有人在Groovy的邮件列表上问Groovy能不能支持Ruby case when表达式: #t#

Ruby case when表达式代码示例:

  1. car = "Patriot"   
  2. manufacturer = case car   
  3. when "Focus": "Ford"   
  4. when "Navigator": "Lincoln"   
  5. when "Camry": "Toyota"   
  6. when "Civic": "Honda"   
  7. when "Patriot": "Jeep"   
  8. when "Jetta": "VW"   
  9. when "Ceyene": "Porsche"   
  10. when "Outback": "Subaru"   
  11. when "520i": "BMW"   
  12. when "Tundra": "Nissan"   
  13. else "Unknown"   
  14. end   
  15. puts "The " + car + " is made by 
    " + manufacturer   
  16. car = "Patriot" 
  17. manufacturer = case car  
  18. when "Focus": "Ford"  
  19. when "Navigator": "Lincoln"  
  20. when "Camry": "Toyota"  
  21. when "Civic": "Honda"  
  22. when "Patriot": "Jeep"  
  23. when "Jetta": "VW"  
  24. when "Ceyene": "Porsche"  
  25. when "Outback": "Subaru"  
  26. when "520i": "BMW"  
  27. when "Tundra": "Nissan"  
  28. else "Unknown"  
  29. end  
  30. puts "The " + car + " is made by 
    " + manufacturer 

然后Guillaume给出了这么一段Ruby case when表达式代码:

 

  1. def car = "Patriot"   
  2. def manufacturer = match(car) {   
  3. when "Focus", "Ford"   
  4. when "Navigator", "Lincoln"   
  5. when "Camry", "Toyota"   
  6. when "Civic", "Honda"   
  7. when "Patriot", "Jeep"   
  8. when "Jetta", "VW"   
  9. when "Ceyene", "Porsche"   
  10. when "Outback", "Subaru"   
  11. when "520i", "BMW"   
  12. when "Tundra", "Nissan"   
  13. otherwise "Unknown"   
  14. }   
  15. println "The $car is made by 
    $manufacturer"   
  16. def match(obj, closure) {   
  17. closure.subject = obj   
  18. closure.when = { value, result ->   
  19. if (value == subject)   
  20. throw new MatchResultException
    (result: result)   
  21. }   
  22. closure.otherwise = { return it }   
  23. closure.resolveStrategy = 
    Closure.DELEGATE_FIRST   
  24. try {   
  25. closure()   
  26. closure.otherwise()   
  27. } catch (MatchResultException r) {   
  28. r.result   
  29. }   
  30. }   
  31. class MatchResultException
     extends RuntimeException {   
  32. def result   
  33. }   
  34. def car = "Patriot" 
  35. def manufacturer = match(car) {  
  36. when "Focus", "Ford"  
  37. when "Navigator", "Lincoln"  
  38. when "Camry", "Toyota"  
  39. when "Civic", "Honda"  
  40. when "Patriot", "Jeep"  
  41. when "Jetta", "VW"  
  42. when "Ceyene", "Porsche"  
  43. when "Outback", "Subaru"  
  44. when "520i", "BMW"  
  45. when "Tundra", "Nissan"  
  46. otherwise "Unknown"  
  47. }  
  48. println "The $car is made 
    by $manufacturer"  
  49. def match(obj, closure) {  
  50. closure.subject = obj 
  51. closure.when = { value, result -> 
  52. if (value == subject)  
  53. throw new MatchResultException
    (result: result)  
  54. }  
  55. closure.otherwise = { return it }  
  56. closure.resolveStrategy = 
    Closure.DELEGATE_FIRST  
  57. try {  
  58. closure()  
  59. closure.otherwise()  
  60. } catch (MatchResultException r) {  
  61. r.result  
  62. }  
  63. }  
  64. class MatchResultException 
    extends RuntimeException {  
  65. def result  

我不是很喜欢里面用异常来控制程序的流程,而且觉得“when "Focus", "Ford"”中间的逗号不够直观,因此就在上面的Ruby case when表达式代码的基础上做了一些修改:

  1. def match(subject, closure) {   
  2. def whenMap = [:], otherwise = null   
  3. closure.when = { map -> whenMap.putAll(map) }   
  4. closure.otherwise = { otherwise = it }   
  5. closure.resolveStrategy = Closure.DELEGATE_FIRST   
  6. closure()   
  7. def result = whenMap.find { condition, 
    value -> subject in condition }   
  8. return result ? result.value : otherwise   
  9. }   
  10. def manufacturer(car) {   
  11. match(car) {   
  12. when "Focus": "Ford"   
  13. when "Navigator": "Lincoln"   
  14. when "Camry": "Toyota"   
  15. when "Civic": "Honda"   
  16. when "Patriot": "Jeep"   
  17. when "Jetta": "VW"   
  18. when "Ceyene": "Porsche"   
  19. when "Outback": "Subaru"   
  20. when "520i": "BMW"   
  21. when "Tundra": "Nissan"   
  22. otherwise "Unknown"   
  23. }   
  24. }   
  25. println "The Patriot is made
     by ${manufacturer('Patriot')}"   
  26. println "The QQ is made by $
    {manufacturer('QQ')}"   
  27. def match(subject, closure) {  
  28. def whenMap = [:], otherwise = null 
  29. closure.when = { map -> whenMap.putAll(map) }  
  30. closure.otherwise = { otherwise = it }  
  31. closure.resolveStrategy = Closure.
    DELEGATE_FIRST  
  32. closure()  
  33. def result = whenMap.find { condition, 
    value -> subject in condition }  
  34. return result ? result.value : otherwise  
  35. }  
  36. def manufacturer(car) {  
  37. match(car) {  
  38. when "Focus": "Ford"  
  39. when "Navigator": "Lincoln"  
  40. when "Camry": "Toyota"  
  41. when "Civic": "Honda"  
  42. when "Patriot": "Jeep"  
  43. when "Jetta": "VW"  
  44. when "Ceyene": "Porsche"  
  45. when "Outback": "Subaru"  
  46. when "520i": "BMW"  
  47. when "Tundra": "Nissan"  
  48. otherwise "Unknown"  
  49. }  
  50. }  
  51. println "The Patriot is made
     by ${manufacturer('Patriot')}"  
  52. println "The QQ is made by $
    {manufacturer('QQ')}" 

以上Ruby case when表达式代码在Groovy 1.6下编译通过。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...