Ruby on rails Rails:Refactoring-json[“数据”][“计数”][“U”后接“if json”&;json[“数据”]&;json[“数据”][“计数”]&;json[“数据”][“计数”][“U”后接“计数”]

Ruby on rails Rails:Refactoring-json[“数据”][“计数”][“U”后接“if json”&;json[“数据”]&;json[“数据”][“计数”]&;json[“数据”][“计数”][“U”后接“计数”],ruby-on-rails,refactoring,Ruby On Rails,Refactoring,问题就在标题里。重构如下内容的最佳方法是什么 json["data"]["counts"]["followed_by"] if json && json["data"] && json["data"]["counts"] && json["data"]["counts"]["followed_by"] 我想返回nil,除非存在json[“data”][“counts”][“counts”][“后跟”]。您可以使用: 我倾向于超长的try链,例如

问题就在标题里。重构如下内容的最佳方法是什么

json["data"]["counts"]["followed_by"] if json &&  json["data"] && json["data"]["counts"] && json["data"]["counts"]["followed_by"]
我想返回
nil
,除非存在
json[“data”][“counts”][“counts”][“后跟”]

您可以使用:

我倾向于超长的
try
链,例如

 json.andand['data'].andand['counts'].andand['followed_by']

在我看来,它只是看起来更好。

1分,用于AND和gem。它看起来肯定比使用长
try
链更好。
 json.andand['data'].andand['counts'].andand['followed_by']