Parameters

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

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

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