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?
def locate_point(mesh, type, xyz)
point = nil
if mesh[type].has_key? xyz
point = mesh[type][xyz]
else
$stderr.print "Warning: Did not locate point in #{type} mesh: "
$stderr.puts xyz.join(’,’)
end
point
end
…
next unless medium_point = locate_point(mesh,‘medium’, xyz)
next unless fine_point = locate_point(mesh,‘fine’, xyz)
…
Cedric
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.
If you can afford them to become instance variables use
instance_variable_set( symbol, object )
Then your method can become
xyz = a key
[:medium, :fine].each do |type|
if data[type].has_key? xyz
instance_variable_set( “@#{type}”.to_sym, data[type] )
else @stderr.print "Warning: Did not locate point in #{type} mesh: " @stderr.puts xyz.join( ‘,’ )
end
end