Nils and method chaining

后端 未结 9 2433
忘了有多久
忘了有多久 2021-02-14 19:21

I\'m just breaking into the ruby world and I could use a helping hand.

Suppose b is nil.

I\'d like the following code to return n

9条回答
  •  不知归路
    2021-02-14 19:46

    Check Ick's maybe:

    a.b.maybe.c("d").maybe.e
    

    or using a block:

    a.b.maybe { |b| b.c("d").e }
    

提交回复
热议问题