parameters..

M

Mc Ieong

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
 
D

David Masover

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
 
M

Mc Ieong

David said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top