[yast-devel] until in ruby
Hi, I was looking for a until loop in Ruby and found this: begin <code> end until <condition> It is heavily used in the generated code. Unfortunately the Ruby inventor himself doesn't like this and would like to remove it, see: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741 AFAIS there's no real until loop in Ruby. The common suggestion is to use loop: loop do <code> break <condition> end Is that true? Or do newer Ruby version offer a real until loop? Regards, Arvin -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
On Wed, 15 Jan 2014 12:09:38 +0100 Arvin Schnell <aschnell@suse.de> wrote:
Hi,
I was looking for a until loop in Ruby and found this:
begin <code> end until <condition>
It is heavily used in the generated code. Unfortunately the Ruby inventor himself doesn't like this and would like to remove it, see:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/6741
AFAIS there's no real until loop in Ruby. The common suggestion is to use loop:
loop do <code> break <condition> end
Is that true? Or do newer Ruby version offer a real until loop?
Regards, Arvin
Hi Arvin, I am not aware of any change in newer ruby. In fact in loops aren't much used in object languages, because you more often iterate over collection so something like collection.each is used. Even if Matz above mention that we would like to remove it, I think that because he do not remove it for eight years, he cannot easily remove it, so you can use it. If you place here code you would like to do, there is always way how to do it without until. Josef -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
On Wed, Jan 15, 2014 at 12:30:52PM +0100, Josef Reidinger wrote:
On Wed, 15 Jan 2014 12:09:38 +0100
I am not aware of any change in newer ruby. In fact in loops aren't much used in object languages, because you more often iterate over collection so something like collection.each is used.
In this case I don't iterate over anything like that.
Even if Matz above mention that we would like to remove it, I think that because he do not remove it for eight years, he cannot easily remove it, so you can use it. If you place here code you would like to do, there is always way how to do it without until.
Sure there always is. The code is: begin ret = UI.UserInput <actions for some ret values> end until <ret some values> if ret == :ok <do stuff> end See CommonWidgetsPopup in StorageProposal.rb. But I don't think the code must improved now. The funny thing is that ret is known after the begin end construct. I didn't expect that. Regards, Arvin -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
On Wed, 15 Jan 2014 12:43:57 +0100 Arvin Schnell <aschnell@suse.de> wrote:
On Wed, Jan 15, 2014 at 12:30:52PM +0100, Josef Reidinger wrote:
On Wed, 15 Jan 2014 12:09:38 +0100
I am not aware of any change in newer ruby. In fact in loops aren't much used in object languages, because you more often iterate over collection so something like collection.each is used.
In this case I don't iterate over anything like that.
Even if Matz above mention that we would like to remove it, I think that because he do not remove it for eight years, he cannot easily remove it, so you can use it. If you place here code you would like to do, there is always way how to do it without until.
Sure there always is.
The code is:
begin
ret = UI.UserInput
<actions for some ret values>
end until <ret some values>
if ret == :ok
<do stuff>
end
See CommonWidgetsPopup in StorageProposal.rb. But I don't think the code must improved now.
The funny thing is that ret is known after the begin end construct. I didn't expect that.
Regards, Arvin
Yes, ruby have different scoping, there is three scopes and the lowest one is method definition. so even variables inside ifs, whiles, begin/ends share this workspace. Reason is that when you need different scope, then you probably want to have it in separate method and do not have long methods with unclear scopes of variables. As I shown in one request, it is also possible to have variable that is not defined. try in irb: if false a = 5 end puts a it do not throw `unknown a`. In current code often used pattern is defined escape symbols - https://github.com/yast/yast-installation/blob/master/src/clients/inst_scc.r... Possible solution is also break or return as you mention. I try this approach in new cio module - https://github.com/yast/yast-cio/blob/initial_version/src/lib/iochannel/chan... Josef -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org To contact the owner, e-mail: yast-devel+owner@opensuse.org
participants (2)
-
Arvin Schnell
-
Josef Reidinger