# Help DRY a couple ifs

**URL:** <https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701>\
**Category:** Ruby\
**Created:** [January 29, 2007, 12:30am UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701 "2007-01-29T00:30:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [January 29, 2007, 12:30am UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/1 "2007-01-29T00:30:20Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [January 29, 2007, 12:36am UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/2 "2007-01-29T00:36:21Z")

</div>

On Jan 28, 2007, at 5:30 PM, Bil K. 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://

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

the\_worlds\_two\_worst\_variable.html for why.

xoxo,  
Andy

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [January 29, 2007, 1:12am UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/3 "2007-01-29T01:12:26Z")

</div>

You could change it like that:

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

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [January 29, 2007, 6:31am UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/4 "2007-01-29T06:31:46Z")

</div>

On Jan 28, 5:27 pm, Bil K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## if data[‘fine’].has\_key? xyz Thanks,
> 
> Bil K.[http://fun3d.larc.nasa.gov](http://fun3d.larc.nasa.gov)

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 )

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/5 "2007-09-25T23:09:57Z")

</div>

Andy L. 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  
> [O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html)  
> for why.

🙂

Later,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/help-dry-a-couple-ifs/83701/6 "2007-09-25T23:09:22Z")

</div>

On 1/29/07, Bil K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Andy L. 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.  
> 🙂

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

But I do like CÃ©dric’s response better.
