[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help DRY a couple ifs

Bil Kleb

1/28/2007 11:28:00 PM

The following snippet is simply nasty, but I
can't see a simple way to DRY it:

if data['medium'].has_key? xyz
medium = data['medium'][xyz]
else
$stderr.print "Warning: Did not locate point in medium mesh: "
$stderr.puts xyz.join(',')
next
end
if data['fine'].has_key? xyz
fine = data['fine'][xyz]
else
$stderr.print "Warning: Did not locate point in fine mesh: "
$stderr.puts xyz.join(',')
next
end

I suspect my lack of eval knowledge may be
the trouble, but that may be a good thing?

Thanks,
--
Bil Kleb
http://fun3d.lar...
3 Answers

andy

1/28/2007 11:36:00 PM

0


On Jan 28, 2007, at 5:30 PM, Bil Kleb wrote:

> The following snippet is simply nasty, but I
> can't see a simple way to DRY it:

Create an outer loop that has "fine" and "medium", and plug that in
to your if blocks.

Also, please consider renaming your variable "data". See http://
www.oreillynet.com/onlamp/blog/2004/03/
the_worlds_two_worst_variable.html for why.

xoxo,
Andy

--
Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance





Bil Kleb

1/29/2007 12:41:00 AM

0

Andy Lester wrote:
>
> Create an outer loop that has "fine" and "medium", and plug that in to
> your if blocks.

Sure, but how do you get medium and fine as variable names?

> Also, please consider renaming your variable "data". See
> http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_var...
> for why.

:)

Later,
--
Bil Kleb
http://fun3d.lar...

William James

1/29/2007 5:28:00 AM

0



On Jan 28, 5:27 pm, Bil Kleb <Bil.K...@NASA.gov> wrote:
> The following snippet is simply nasty, but I
> can't see a simple way to DRY it:
>
> if data['medium'].has_key? xyz
> medium = data['medium'][xyz]
> else
> $stderr.print "Warning: Did not locate point in medium mesh: "
> $stderr.puts xyz.join(',')
> next
> end
> if data['fine'].has_key? xyz
> fine = data['fine'][xyz]
> else
> $stderr.print "Warning: Did not locate point in fine mesh: "
> $stderr.puts xyz.join(',')
> next
> end
>
> I suspect my lack of eval knowledge may be
> the trouble, but that may be a good thing?
>
> Thanks,
> --
> Bil Klebhttp://fun3d.lar...

If the hash isn't autovivifying (if refering to a non-existant entry
won't create an entry):

medium = data['medium'][xyz] or
( $stderr.print "Warning: Did not locate point in medium mesh: "
$stderr.puts xyz.join(',')
next )