Common Ruby Idioms

前端 未结 15 2039
星月不相逢
星月不相逢 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:21

    a = (b && b.attribute) || "default"
    

    is roughly:

    if ( ! b.nil? && ! b == false) && ( ! b.attribute.nil? && ! b.attribute.false) a = b
    else a = "default"
    

    I use this when b is a record which may or may not have been found, and I need to get one of its attributes.

提交回复
热议问题