Common Ruby Idioms

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

    I would suggest reading through the code of popular and well designed plugins or gems from people you admire and respect.

    Some examples I've run into:

    if params[:controller] == 'discussions' or params[:controller] == 'account'
      # do something here
    end
    

    corresponding to

    if ['account', 'discussions'].include? params[:controller]
      # do something here
    end
    

    which later would be refactored to

    if ALLOWED_CONTROLLERS.include? params[:controller]
      # do something here
    end
    

提交回复
热议问题