# Parameters

**URL:** <https://www.ruby-forum.com/t/parameters/182621>\
**Category:** Ruby\
**Created:** [January 31, 2010, 7:28am UTC](https://www.ruby-forum.com/t/parameters/182621 "2010-01-31T07:28:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcieong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mcieong](https://www.ruby-forum.com/u/mcieong)\
**Post date:** [January 31, 2010, 7:28am UTC](https://www.ruby-forum.com/t/parameters/182621/1 "2010-01-31T07:28:33Z")

</div>

Hi! I am a newbie in Ruby programming. I wrote the following functions  
and they works fine, but it seems very stupid of not using just one  
parameterized function to update the same fields(:first\_name,  
:last\_name), can everyone give me some hints about that ?

def update\_first\_name  
if request.post?  
if !params[:data].nil?  
Employee.update(params[:id], :first\_name =\> params[:data])  
end  
end  
render(:nothing =\> true)  
end

def update\_last\_name  
if request.post?  
if !params[:data].nil?  
Employee.update(params[:id], :last\_name =\> params[:data])  
end  
end  
render(:nothing =\> true)  
end

Thanks in advance.

Ieong

---

<div class="post-metadata">

**Author:** ![mcieong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mcieong](https://www.ruby-forum.com/u/mcieong)\
**Post date:** [January 31, 2010, 8:07am UTC](https://www.ruby-forum.com/t/parameters/182621/2 "2010-01-31T08:07:42Z")

</div>

On Sunday 31 January 2010 12:28:54 am Mc Ieong wrote:

> Hi! I am a newbie in Ruby programming. I wrote the following functions  
> and they works fine, but it seems very stupid of not using just one  
> parameterized function to update the same fields(:first\_name,
> 
> :last\_name), can everyone give me some hints about that ?

Should be obvious, shouldn’t it?

def update(which\_name)  
if request.post?  
if !params[:data].nil?  
Employee.update(params[:id], which\_name =\> params[:data])  
end  
end  
render(:nothing =\> true)  
end

Remember, symbols are just objects. There’s nothing special about using  
symbols in keyword arguments, and nothing stopping you from using other  
things. The only reason you never see library code doing this:

Foo.bar “some option” =\> “some value”

is that it’s much less efficient than using a symbol. But there’s no  
reason  
you couldn’t use a variable, a number, or anything else in there –  
what’s  
important is the =\> operator.

As an aside, you can keep your existing API if you need it:

def update\_first\_name  
update :first\_name  
end  
def update\_last\_name  
update :last\_name  
end

If for some reason you have a lot of these:

[:first\_name, :last\_name].each do |which\_name|  
define\_method “update\_#{which\_name}” do  
update which\_name  
end  
end

---

<div class="post-metadata">

**Author:** ![mcieong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mcieong](https://www.ruby-forum.com/u/mcieong)\
**Post date:** [January 31, 2010, 9:05am UTC](https://www.ruby-forum.com/t/parameters/182621/3 "2010-01-31T09:05:10Z")

</div>

David M. wrote:

> On Sunday 31 January 2010 12:28:54 am Mc Ieong wrote:
> 
> > Hi! I am a newbie in Ruby programming. I wrote the following functions  
> > and they works fine, but it seems very stupid of not using just one  
> > parameterized function to update the same fields(:first\_name,
> > 
> > :last\_name), can everyone give me some hints about that ?
> 
> Should be obvious, shouldn’t it?
> 
> def update(which\_name)  
> if request.post?  
> if !params[:data].nil?  
> Employee.update(params[:id], which\_name =\> params[:data])  
> end  
> end  
> render(:nothing =\> true)  
> end
> 
> Remember, symbols are just objects. There’s nothing special about using  
> symbols in keyword arguments, and nothing stopping you from using other  
> things. The only reason you never see library code doing this:
> 
> Foo.bar “some option” =\> “some value”
> 
> is that it’s much less efficient than using a symbol. But there’s no  
> reason  
> you couldn’t use a variable, a number, or anything else in there –  
> what’s  
> important is the =\> operator.
> 
> As an aside, you can keep your existing API if you need it:
> 
> def update\_first\_name  
> update :first\_name  
> end  
> def update\_last\_name  
> update :last\_name  
> end
> 
> If for some reason you have a lot of these:
> 
> [:first\_name, :last\_name].each do |which\_name|  
> define\_method “update\_#{which\_name}” do  
> update which\_name  
> end  
> end

Dear David,

Thank you very much for your detail explanation, and give me a lot of  
coding alternaties which I didn’t know before.

Thank you again for your help!

Ieong
