The Crystal Programming Language Forum

Redo a loop iteration

Unlike Ruby, Crystal seems to lack the redo statement. Do you think it would be useful to write code like this?

some_items.each do |item|
  case process(item)
  when "foo" then true
  when "bar" then false
  else redo
  end
end

Am I missunderstanding something, or would “redo” do exactly just lead to an endless loop here?

Because if the item doesn’t fit the first time, why would it fit the second time?

The value which is returned by process(item) can change with time, so it’s not necessary an endless loop.

ahh, i see. You could just “redo” inside process(item) and only return when its done?

But I see the general idea.

Yes, it could be that way (the example is not the best one). And it’s possible to overcome the lack of redo, but the code becomes unnecessary bulky, IMO.

Yeah, it’s a deliberate choice because I never used it in Ruby, nor my peers. It could be done but since its use case is so limited and there are ways to do it without a language keyword we will probably never incorporate it in the language (though now that I think about it, it’s just a jump to the beginning of the iteration, but there will be new typing rules).

It’s just one more loop:

some_items.each do |item|
  loop do
    case process(item)
    when "foo" then break true
    when "bar" then break false
    end
  end
end
2 Likes