Help DRY a couple ifs

B

Bil Kleb

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,
 
W

William James

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,

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 )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top