Ruby best practice : if not empty each do else in one operator

后端 未结 7 2498
名媛妹妹
名媛妹妹 2021-02-18 16:04

1.I can\'t find an elegant way to write this code:

if array.empty?
  # process empty array
else
  array.each do |el|
    # process el
  end
end

7条回答
  •  北海茫月
    2021-02-18 16:15

    I think there is no much more elegant or readable way to write this. Any way to somehow combine an iteration with a condition will just result in blackboxed code, meaning: the condition will just most likely be hidden in an Array extension.

提交回复
热议问题