Common Ruby Idioms

前端 未结 15 1975
星月不相逢
星月不相逢 2020-12-07 07:13

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code)

However, inspired by this question: Ruby Code ex

15条回答
  •  遥遥无期
    2020-12-07 07:36

    I like this:

    str = "Something evil this way comes!"
    regexp = /(\w[aeiou])/
    
    str[regexp, 1] # <- This
    

    Which is (roughly) equivalent to:

    str_match = str.match(regexp)
    str_match[1] unless str_match.nil?
    

    Or at least that's what I've used to replace such blocks.

提交回复
热议问题