# Suggestions needed to go DRY

**URL:** <https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105>\
**Category:** Ruby\
**Created:** [April 16, 2007, 7:28am UTC](https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105 "2007-04-16T07:28:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [April 16, 2007, 7:28am UTC](https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105/1 "2007-04-16T07:28:42Z")

</div>

Hi All,

```
 Recently I was in a situation where I needed to add a functionality 

```

to  
a method but I couldn’t change the method signature as people in my team  
were using the same method at many places.I joined the team late so it  
was  
not envisioned that my part will bring in need for few changes.I  
couldn’t  
create a new method as  
it wont be dry.The case was I needed to merge few key,value pairs in  
side  
the hash present in method.These were not same all the time.

the method was–\>

def doThing var1,var2  
html\_options #already defined the basic things needed for  
functionality  
in this hash  
end

I made changes within the method --\>

def doThing var1,var2  
html\_options.merge! :onclick =\> yield if block\_given?  
end

so calling it doThing(“one”,“two”) {“methodCalledOnClick()”} did the  
trick  
other people were happy using it doThing “one”,“two”

I hope I was able to explain my situation.  
I’ll appreciate if you give few inputs how you people would have done  
the  
same thing or guide me to do this in a better fashion.

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [April 16, 2007, 7:48am UTC](https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105/2 "2007-04-16T07:48:43Z")

</div>

On Mon, Apr 16, 2007 at 02:27:56PM +0900, gaurav bagga wrote:

> html\_options.merge! :onclick =\> yield if block\_given?  
> end
> 
> so calling it doThing(“one”,“two”) {“methodCalledOnClick()”} did the trick  
> other people were happy using it doThing “one”,“two”

Another way is to have an optional argument:

def doThing(var1, var2, opts={})  
html\_options.merge!(opts)  
end

or:

def doThing(var1, var2, opts=nil)  
html\_options.merge!(opts) if opts  
end

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [April 16, 2007, 9:22am UTC](https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105/3 "2007-04-16T09:22:50Z")

</div>

On 4/16/07, gaurav bagga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def doThing var1,var2  
> html\_options.merge! :onclick =\> yield if block\_given?  
> end
> 
> so calling it doThing(“one”,“two”) {“methodCalledOnClick()”} did the trick  
> other people were happy using it doThing “one”,“two”

This scales slightly better (think of adding several options):

def doThing var1, var2  
yield html\_options  
…  
end

use as

doThing(var1, var2) {|opts|  
opts[:onclick] =foo  
opts[:css] = bar  
…  
}

martin

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [April 17, 2007, 6:41am UTC](https://www.ruby-forum.com/t/suggestions-needed-to-go-dry/92105/4 "2007-04-17T06:41:25Z")

</div>

Thanks Brian and Martin for feedback  
That was nice

Martin your idea for scalability I liked  
Brian optional arguments taught me something new I overlooked that

Regards  
Gaurav
