ruby syntactic sugar: dealing with nils

前端 未结 6 1788
天命终不由人
天命终不由人 2021-02-10 02:45

probably asked already but I couldn\'t find it.. here are 2 common situation (for me while programming rails..) that are frustrating to write in ruby:

\"a string         


        
6条回答
  •  伪装坚强ぢ
    2021-02-10 03:03

    For the first I'd recommend ick's maybe (equivalent to andand)

    "a string".match(/abc(.+)abc/).maybe[1]
    

    I am not sure I understand the second one, you want this?

    var = something.very.long.and.tedious.to.write || something.other
    

提交回复
热议问题