I’ve always been searching for something like Python’s while / else struct in Ruby to improve my code.
That means that the loop is executed and if the condition in the loop hasn’t been true any time, then it returns the value in the else statement.
In ruby, I can do like this :
if @items.empty?
"Empty"
else
@items.each do |item|
item
end
end
So is there a way to improve this ?
Thank you in advance.
Hm, you could write it as a ternary:
You may want to do something more useful in your block though, since
eachis executed for its side effects and returns the original receiver.Update as per your comment: I guess the closest you could get is something like