# How to add parameter and condition in function dynamically?

**URL:** <https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651>\
**Category:** Rails\
**Created:** [April 24, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651 "2008-04-24T13:48:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [April 24, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651/1 "2008-04-24T13:48:04Z")

</div>

Hi All,  
I have one function in that function i wanna add parameters and  
conditions dynamically.

I hope you got me if yes please tell me what should i do.

Thanks  
Varun K.

---

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [April 24, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651/2 "2008-04-24T13:56:19Z")

</div>

On 24 Apr 2008, at 12:48, Varun G. wrote:

> Hi All,  
> I have one function in that function i wanna add parameters and  
> conditions dynamically.
> 
> I hope you got me if yes please tell me what should i do.

You’re going to have to be more specific.

Fred

---

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [April 24, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651/3 "2008-04-24T16:24:56Z")

</div>

If you have:

def foo(\*bar)  
bar.class # =\> Array  
end

So the parameters becomes an array of values, including an empty array  
if nothing is passed in.

Then you can do stuff like:

foo  
foo “one”, “two”  
foo :a =\> “b”  
etc.

Is that what you were looking for?

-Danimal

On Apr 24, 5:55 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![varunkumargoel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed8c4c/32.png) [@varunkumargoel](https://www.ruby-forum.com/u/varunkumargoel)\
**Post date:** [April 24, 2008, 6:34pm UTC](https://www.ruby-forum.com/t/how-to-add-parameter-and-condition-in-function-dynamically/196651/4 "2008-04-24T18:34:52Z")

</div>

Another pseudo-random guess…

I’ve had several places where I’ve done something like this in a  
controller:

FIND\_OPTIONS = {:limit=\>10, :offset=\>0, :order=\>:name}  
def index  
find\_options = FIND\_OPTIONS.dup  
find\_options.reverse\_merge :name=\>params[:name] if  
params[:name].blank?  
… other options…  
SomeModel.find(:all, find\_options)  
end

That allows you to search only for arguments that are supplied by the  
user (ignoring blanks as don’t cares).

If you want to only add/update data as returned from a form  
submission… Rails already does that for you.
