成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Ruby case when表達式實際應用解析

開發 開發工具
我們在這篇文章中為大家介紹的是有關Ruby case when表達式的一些基本使用技巧。比如是否能夠在Groovy中得到支持等。

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下編譯通過。

責任編輯:曹凱 來源: javaeye.com
相關推薦

2009-12-17 10:39:01

Ruby數學表達式

2009-12-16 09:29:26

Ruby布爾表達式

2009-12-14 11:33:59

Ruby正則表達式

2024-05-15 08:09:23

2010-07-09 09:11:33

Ruby正則表達式

2010-04-28 18:17:16

Oracle CASE

2010-11-12 13:20:31

SQL Server

2024-02-02 12:41:33

表達式語法Cron

2020-10-14 10:18:05

Python三元表達式代碼

2010-07-13 17:03:53

Perl正則表達式

2010-08-09 13:58:59

Flex正則表達式

2010-07-28 11:06:41

Flex正則表達式

2010-07-14 09:37:46

Perl正則表達式

2011-06-16 15:28:31

正則表達式

2024-09-14 09:18:14

Python正則表達式

2024-10-15 16:53:07

2014-01-05 17:41:09

PostgreSQL表達式

2011-07-11 12:33:30

JAVA

2009-09-16 18:08:14

正則表達式匹配單詞

2009-12-18 13:57:35

Ruby正則表達式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 九九av| 国产精品美女久久久久久免费 | 久色网| av入口| 国产色99 | 欧美男人天堂 | a级黄色片在线观看 | 免费成人av网站 | 国产精品久久久久免费 | 91人人澡人人爽 | 亚洲一区av | 亚洲视频网| av网站免费观看 | 亚洲欧美视频一区二区 | 日韩精品一二三区 | 国产精品国产三级国产aⅴ原创 | 午夜在线| 国产精品高潮呻吟久久 | 麻豆亚洲 | 亚洲欧美综合精品久久成人 | 精久久久| 伊人网一区 | 国产不卡一区在线观看 | 野狼在线社区2017入口 | 人人干人人看 | 午夜精品 | 玖玖久久 | 中文字幕在线观看www | av天天干 | 日韩乱码一二三 | 久久国内精品 | 久久se精品一区精品二区 | 一区视频在线 | 国产成人高清成人av片在线看 | 精品成人一区 | 999国产视频 | 国产在线观看一区二区三区 | 99久久夜色精品国产亚洲96 | 久久精品免费一区二区三 | 久久国产精品一区二区三区 | 中文字幕一区二区三区四区五区 |