Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to "break" a "case-when" ?

Reply
Thread Tools

How to "break" a "case-when" ?

 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-17-2008
SGksIGlzIG5vdCBwb3NzaWJsZSB0byB0ZXJtaW5hdGUgdGhlIG JvZHkgb2YgYSAid2hlbiIgc3Rh
bWVudD8KCkkgbWVhbjoKCiAgY2FzZSBudW0KICB3aGVuIDE6Ci AgICBwdXRzICJJcyAxICEhISEi
CiAgICBicmVhawogICAgcHV0cyAiRG9uJ3Qgd3JpdGUgaXQiCi AgZW5kCgogIHB1dHMgIldyaXRl
IGl0IgoKYnV0IHVuZm9ydHVuYXRlbGx5IHRoZSAiYnJlYWsiIG dpdmVzIGFuIGVycm9yLgoKVGhh
bmtzIGZvciBhbnkgc3VnZ2VzdGlvbi4KCi0tIApJw7Fha2kgQm F6IENhc3RpbGxvCjxpYmNAYWxp
YXgubmV0Pgo=

 
Reply With Quote
 
 
 
 
Peña, Botp
Guest
Posts: n/a
 
      04-17-2008
RnJvbTogScOxYWtpIEJheiBDYXN0aWxsbyBbbWFpbHRvOmliY0 BhbGlheC5uZXRdIA0KIyBIaSwg
aXMgbm90IHBvc3NpYmxlIHRvIHRlcm1pbmF0ZSB0aGUgYm9keS BvZiBhICJ3aGVuIiBzdGFtZW50
Pw0KIyAgIGNhc2UgbnVtDQojICAgd2hlbiAxOg0KIyAgICAgcH V0cyAiSXMgMSAhISEhIg0KIyAg
ICAgYnJlYWsNCiMgICAgIHB1dHMgIkRvbid0IHdyaXRlIGl0Ig 0KIyAgIGVuZA0KIyAgIHB1dHMg
IldyaXRlIGl0Ig0KDQphZmFpaywgbm8uIGJ1dCB3aHkgd291bG QgeW91IHdhbnQgdG8gZG8gdGhh
dD8gYXJlIHlvdSBwb3J0aW5nIGMgY29kZXM/DQoNCmtpbmQgcmVnYXJkcyAtYm90cA0K

 
Reply With Quote
 
 
 
 
Stefan Lang
Guest
Posts: n/a
 
      04-17-2008
2008/4/17, I=F1aki Baz Castillo <(E-Mail Removed)>:
> Hi, is not possible to terminate the body of a "when" stament?
>
> I mean:
>
> case num
> when 1:
> puts "Is 1 !!!!"
> break
> puts "Don't write it"
> end
>
> puts "Write it"
>
> but unfortunatelly the "break" gives an error.


break is only for iteration. What are you trying to do?
The example you've given is not a compelling use case,
since you can simply remove the line after break.

Also, Ruby's case/when doesn't fall through when clauses.

case 1
when 1
puts "it's 1"
when 2
puts "this is not printed"
end

Stefan

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-17-2008
MjAwOC80LzE3LCBTdGVmYW4gTGFuZyA8cGVyZmVjdGx5Lm5vcm 1hbC5oYWNrZXJAZ21haWwuY29t
PjoKCj4gYnJlYWsgaXMgb25seSBmb3IgaXRlcmF0aW9uLiBXaG F0IGFyZSB5b3UgdHJ5aW5nIHRv
IGRvPwo+ICBUaGUgZXhhbXBsZSB5b3UndmUgZ2l2ZW4gaXMgbm 90IGEgY29tcGVsbGluZyB1c2Ug
Y2FzZSwKPiAgc2luY2UgeW91IGNhbiBzaW1wbHkgcmVtb3ZlIH RoZSBsaW5lIGFmdGVyIGJyZWFr
LgoKT2ssIEkgd3JpdGUgYSBiZXR0ZXIgZXhhbXBsZToKCgogIH ZhciA9IC4uLnNvbWV0aGluZy4u
LgoKIGNhc2UgbnVtCiAgd2hlbiAxOgogICAgcHV0cyAidmFyIG 5vdCB2YWxpZCAhISEhISIgaWYg
KCB2YXIgPCAwIHx8IHZhciA+IDEwICkKICAgIGJyZWFrCiAgIC Bkb19ub3JtYWxfc3R1ZmYKICAg
IC4uLgogICAgLi4uCiAgICBlbmRfbm9ybWFsX3N0dWZmCiAgZW 5kCgogIHB1dHMgIldyaXRlIGl0
IgoKCkkganVzdCB3YW50IGRvX25vcm1hbF9zdHVmZiBpbiBjYX NlICJ2YXIiIGlzIHZhbGlkLgpZ
ZXMsIEkgY2FuIHB1dCBhIGJpZyAiaWYiIHN0YW1lbnQgYW5kIH NvIGJ1dCBJIHdvdWxkIGxpa2Ug
bm90IHRvIGRvIGl0CnNpbmNlIGl0IG1ha2VzIGEgYml0IHVnbH kgdGhlIGNvZGUuCgoKLS0gCknD
sWFraSBCYXogQ2FzdGlsbG8KPGliY0BhbGlheC5uZXQ+Cg==

 
Reply With Quote
 
Peter Hickman
Guest
Posts: n/a
 
      04-17-2008
Iñaki Baz Castillo wrote:
> 2008/4/17, Stefan Lang <(E-Mail Removed)>:
>
>
>> break is only for iteration. What are you trying to do?
>> The example you've given is not a compelling use case,
>> since you can simply remove the line after break.
>>

>
> Ok, I write a better example:
>
>
> var = ...something...
>
> case num
> when 1:
> puts "var not valid !!!!!" if ( var < 0 || var > 10 )
> break
> do_normal_stuff
> ...
> ...
> end_normal_stuff
> end
>
> puts "Write it"
>
>
> I just want do_normal_stuff in case "var" is valid.
> Yes, I can put a big "if" stament and so but I would like not to do it
> since it makes a bit ugly the code.
>
>
>


But in this example the code between do_normal_stuff and
end_normal_stuff would never be executed, not even in C!

Why not write something simpler

case num
when 1:
if 0 <= var and var <= 10) then
do_normal_stuff
...
...
end_normal_stuff
puts "Write it"
end


I think you are trying to be too clever here. Stick to the simple stuff.

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-17-2008
MjAwOC80LzE3LCBQZXRlciBIaWNrbWFuIDxwZXRlckBzZW1hbn RpY28uY29tPjoKCj4gIEJ1dCBp
biB0aGlzIGV4YW1wbGUgdGhlIGNvZGUgYmV0d2VlbiBkb19ub3 JtYWxfc3R1ZmYgYW5kIGVuZF9u
b3JtYWxfc3R1ZmYKPiB3b3VsZCBuZXZlciBiZSBleGVjdXRlZC wgbm90IGV2ZW4gaW4gQyEKCk9w
c3MsIHNvcnJ5LCBteSBtaXN0YWtlLiBUaGlzIHdvdWxkIGJlOg oKdmFyID0gLi4uc29tZXRoaW5n
Li4uCgogIGNhc2UgbnVtCiAgd2hlbiAxOgogICAgaWYgKCB2YX IgPCAwIHx8IHZhciA+IDEwICkK
ICAgICAgICBwdXRzICJ2YXIgbm90IHZhbGlkICEhISEhIgogIC AgICAgIGJyZWFrCiAgICBlbmQK
ICAgIGRvX25vcm1hbF9zdHVmZgogICAgLi4uCiAgICAuLi4KIC AgIGVuZF9ub3JtYWxfc3R1ZmYK
ICBlbmQKCiAgcHV0cyAiV3JpdGUgaXQiCgoKCj4gIFdoeSBub3 Qgd3JpdGUgc29tZXRoaW5nIHNp
bXBsZXIKPgo+ICBjYXNlIG51bQo+ICB3aGVuIDE6Cj4gICBpZi AwIDw9IHZhciBhbmQgdmFyIDw9
IDEwKSB0aGVuCj4gICAgZG9fbm9ybWFsX3N0dWZmCj4gICAgLi 4uCj4gICAgLi4uCj4gICAgZW5k
X25vcm1hbF9zdHVmZgo+ICAgIHB1dHMgIldyaXRlIGl0Igo+IC AgZW5kCgpZZXMsIGJ1dCB3aGF0
IEkgd2FzIHRyeWluZyB0byBhdm9pZCBpcyB0byBlbmNsb3NlIG FsbCB0aGUgInJlYWwiIHN0dWZm
Cm5pIGEgImlmIiBzdGFtZW50IHNpbmNlIGl0J3MgaW4gZmFjdC B0aGUgZGVmYXVsdCBiZWhhdmlv
dXIgYW5kIHRoYXQKd2lsbCBiZSBleGVjdXRlZCA5OSUgb2YgdG ltZXMuCgpBbnl3YXkgSSB1bmRl
cnN0YW5kIHRoYXQgaXMgdGhlIG9ubHkgd2F5LgoKVGhhbmtzIG EgbG90IGZvciBhbGwuIDopCgoK
LS0gCknDsWFraSBCYXogQ2FzdGlsbG8KPGliY0BhbGlheC5uZX Q+Cg==

 
Reply With Quote
 
Stefan Lang
Guest
Posts: n/a
 
      04-17-2008
2008/4/17, I=F1aki Baz Castillo <(E-Mail Removed)>:
> 2008/4/17, Stefan Lang <(E-Mail Removed)>:
>
>
> > break is only for iteration. What are you trying to do?
> > The example you've given is not a compelling use case,
> > since you can simply remove the line after break.

>
>
> Ok, I write a better example:
>
>
> var =3D ...something...
>
> case num
> when 1:
> puts "var not valid !!!!!" if ( var < 0 || var > 10 )
> break
> do_normal_stuff
> ...
> ...
> end_normal_stuff
> end
>
> puts "Write it"
>
>
> I just want do_normal_stuff in case "var" is valid.
> Yes, I can put a big "if" stament and so but I would like not to do it
> since it makes a bit ugly the code.


Well, since Ruby doesn't support it, you must find other
ways to structure the code in each case. Exceptions might
help in the bigger picture, splitting it up in smaller methods, etc.

You can use catch/throw for this:

catch(:invalid_var) {
case num
when 1
if var < 0 || var > 10
puts "var not valid !!!!!"
throw(:invalid_var)
end
do_normal_stuff
...
...
end_normal_stuff
end
}
puts "Write it"

But be careful, catch/throw is dynamically scoped.
"ri catch" has more info.

Stefan

 
Reply With Quote
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-17-2008
MjAwOC80LzE3LCBTdGVmYW4gTGFuZyA8cGVyZmVjdGx5Lm5vcm 1hbC5oYWNrZXJAZ21haWwuY29t
PjoKPiBCdXQgYmUgY2FyZWZ1bCwgY2F0Y2gvdGhyb3cgaXMgZH luYW1pY2FsbHkgc2NvcGVkLgo+
ICAicmkgY2F0Y2giIGhhcyBtb3JlIGluZm8uCgp0aGFua3MgZm 9yIHRoYXQgaW5mbyA7KQoKLS0g
CknDsWFraSBCYXogQ2FzdGlsbG8KPGliY0BhbGlheC5uZXQ+Cg ==

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      04-17-2008
2008/4/17, Stefan Lang <(E-Mail Removed)>:
> 2008/4/17, I=F1aki Baz Castillo <(E-Mail Removed)>:
>
> > 2008/4/17, Stefan Lang <(E-Mail Removed)>:


> Well, since Ruby doesn't support it, you must find other
> ways to structure the code in each case. Exceptions might
> help in the bigger picture, splitting it up in smaller methods, etc.
>
> You can use catch/throw for this:


I would recommend using an "ordinary" exception - because that's what it is=
 
Reply With Quote
 
Peña, Botp
Guest
Posts: n/a
 
      04-18-2008
RnJvbTogScOxYWtpIEJheiBDYXN0aWxsbyBbbWFpbHRvOmliY0 BhbGlheC5uZXRdIA0KIyB2YXIg
PSAuLi5zb21ldGhpbmcuLi4NCiMgICBjYXNlIG51bQ0KIyAgIH doZW4gMToNCiMgICAgIGlmICgg
dmFyIDwgMCB8fCB2YXIgPiAxMCApDQojICAgICAgICAgcHV0cy AidmFyIG5vdCB2YWxpZCAhISEh
ISINCiMgICAgICAgICBicmVhaw0KIyAgICAgZW5kDQojICAgIC Bkb19ub3JtYWxfc3R1ZmYNCiMg
ICAgIC4uLg0KIyAgICAgLi4uDQojICAgICBlbmRfbm9ybWFsX3 N0dWZmDQojICAgZW5kDQojICAg
cHV0cyAiV3JpdGUgaXQiDQojLi4uLg0KIyBZZXMsIGJ1dCB3aG F0IEkgd2FzIHRyeWluZyB0byBh
dm9pZCBpcyB0byBlbmNsb3NlIGFsbCB0aGUgInJlYWwiIHN0dW ZmDQojIG5pIGEgImlmIiBzdGFt
ZW50IHNpbmNlIGl0J3MgaW4gZmFjdCB0aGUgZGVmYXVsdCBiZW hhdmlvdXIgYW5kIHRoYXQNCiMg
d2lsbCBiZSBleGVjdXRlZCA5OSUgb2YgdGltZXMuDQojIEFueX dheSBJIHVuZGVyc3RhbmQgdGhh
dCBpcyB0aGUgb25seSB3YXkuDQoNCmhvdyBhYm91dCBhbm90aG VyIGNhc2Ugd2F5LCANCg0KdmFy
ID0gLi4uc29tZXRoaW5nLi4uDQpjYXNlIA0KICB3aGVuIG51bT 09MSBhbmQgKCB2YXIgPCAwIHx8
IHZhciA+IDEwICkNCiAgICAgcHV0cyAidmFyIG5vdCB2YWxpZC Bmb3IgbnVtPT0xICEhISEhIg0K
ICB3aGVuIG90aGVyX3N0dWZmX2NvbmRpdGlvbg0KICB3aGVuIC 4uLi4NCiAgICAgLi4uDQogIGVs
c2UNCiAgICBlbHNlX3N0dWZmDQplbmQNCnB1dHMgIldyaXRlIG l0Ig0KDQpraW5kIHJlZ2FyZHMg
LWJvdHANCg==

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments