我有以下代码,但无法运行:
%w(acp bcp ccp tcp).each do |kind|
define_method(kind+"_name") { send "[#{kind}_title ? #{kind}_title.title : '',#{kind}_firstname,#{kind}_lastname].join(' ')" }
end
运行时:
> Form.last.acp_name
NoMethodError: undefined method `[acp_title ? acp_title.title : '',acp_firstname,acp_lastname].join(' ')' for #<Form:0x108daabe8>
任何人都知道如何修复它以便作为代码运行而不是字面意思吗?
提前致谢...
答案 0 :(得分:4)
%w(acp bcp ccp tcp).each do |kind|
define_method("#{ kind }_name") do
title = send("#{ kind }_title") || ''
array = [ title, send("#{ kind }_firstname"), send("#{ kind }_lastname")]
array.join(' ')
end
end