Ruby on rails 意外语法错误
给定来自我的自定义模型的以下代码:Ruby on rails 意外语法错误,ruby-on-rails,ruby,syntax-error,Ruby On Rails,Ruby,Syntax Error,给定来自我的自定义模型的以下代码: def categorize @cv = Cv.find(params[:cv_id], :include => [:desired_occupations, :past_occupations, :educational_skills]) @menu = :second @language = Language.resolve(:code => :en, :name => :en) categorizer
def categorize
@cv = Cv.find(params[:cv_id], :include => [:desired_occupations, :past_occupations, :educational_skills])
@menu = :second
@language = Language.resolve(:code => :en, :name => :en)
categorizer = CvCategorizer.new @cv, @language
categorizer.prepare_occupations
categorizer.prepare_occupation_skills
categorizer.prepare_education_skills
# fetch the data
@occupation_hashes = categorizer.occupations
@skill_hashes = categorizer.skills
# Sort the hashes
@occupation_hashes.sort! { |x,y| y.score <=> x.score}
@skill_hashes.sort! { |x,y| y.score <=> x.score}
@max = @skill_hashes.first.score
@min = @skill_hashes.last.score
end
def分类
@cv=cv.find(参数[:cv\u id],:include=>[:期望的职业,:过去的职业,:教育技能])
@菜单=:秒
@language=language.resolve(:code=>:en,:name=>:en)
categorizer=CvCategorizer.new@cv,@language
分类。准备职业
分类。准备职业技能
分类。准备教育和技能
#获取数据
@职业\哈希=分类程序。职业
@skill\u hashes=分类程序.skills
#对散列进行排序
@职业分类!{| x,y | y.score x.score}
@skill\u hashes.sort!{| x,y | y.score x.score}
@max=@skill\u hashes.first.score
@min=@skill\u hashes.last.score
结束
代码创建了CvCategorizer类的一个新实例,并按顺序调用了三个prepare方法。他们都用从数据库中检索到的数据做一些古怪的事情。代码如下所示:
# = CvCategorizer
# This class will handle the categorizing of a CV based upon the skills and occupations found in
# the CV. Because the controller originally had a huge chunk of code, this class will break up that
# login into seperate function calls and keep everything inside variables for easy access.
class CvCategorizer
# initializes a new instance of the CvCategorizer
def initialize cv, language
@cv = cv
@language = language
@occupations = []
@skills = []
end
# Prepares the occupation array by looking at the stored CV and collecting
# all the desired occupations and past occupations. These are stored inside
# the internal occupation array as a uniq list.
def prepare_occupations
all_occupations = @cv.desired_occupations.all(:include => :labels) + @cv.past_occupations.all(:include => :labels)
all_occupations.each do |occupation|
oc = OccupationCategory.new
oc.is_desired_work?= @cv.desired_occupations.include?(occupation)
oc.is_work_experience?= @cv.past_occupations.include?(occupation)
oc.occupation = occupation
if !@occupations.include?(oc)
@occupations << oc
else
obj = @occupations.select(oc)
obj.score += 1
obj.occupations= (obj.occupations & oc).uniq
end
end
=begin
all_occupations = @cv.desired_occupations.all(:include => :labels) + @cv.past_occupations.all(:include => :labels)
all_occupations.each do |occupation|
section = []
section << "Desired Work" if @cv.desired_occupations.include? occupation
section << "Work experience" if @cv.past_occupations.include? occupation
unless (array = @occupations.assoc(occupation)).blank?
array[1]+= 1
array[2] = (array[2] & section).uniq
else
@occupations << [occupation, 1, section, []]
end
end
=end
end
# Prepares the occupation skills of the CV by looping over all the stored
# occupations and retrieving the skills for them and storing them in the
# skills array.
def prepare_occupation_skills
# Loop over all the OccupationCategory objects currently present in the Categorizer.
@occupations.each do |oc|
# For each OccupationCategory object, retrieve all the associated skills, and
# include their label as well.
oc.occupation.skills.all(:include => :labels).each do |skill|
# Get the label of the current concept we're working with.
label = oc.occupation.concept.label(@language).value
# Check if the @skills array already contains a SkillCategory object with the
# skill we're currently checking.
if (sc = @skills.select{|scs| scs.skill == skill}).blank?
# The skill was not found, so create a new entry with the SkillCategory class and set the
# correct values for the various properties
sc = SkillCategory.new
sc.labels << label
sc.score= 1
sc.is_occupation_skill? = true
sc.skill= skill
else
# The skill was found in one of the SkillCategory objects. So we update the score by
# 1 and store the label of the current concept, unless that label is already present.
sc.labels << label unless sc.labels.include?(label)
sc.is_occupation_skill?= true
sc.score+= 1
end
end
end
=begin
@occupations.each do |array|
array[0].skills.all(:include => :labels).each do |skill|
unless (skill_array = @skills.assoc skill).blank?
label = array[0].concept.label(@language).value
skill_array[1]+= 1
skill_array[3] << label unless skill_array[3].include? label
else
@skills << [skill, 1, [], [array[0].concept.label(@language).value]]
end
end
end
=end
end
# Prepares the education skills by checking the CV and adding them to the
# skills array
def prepare_education_skills
# Loop over all the educational skills that are currently associated to the CV.
@cv.educational_skills.all(:include => :labels).each do |skill|
# Check if the @skills array already contains a SkillCategory object with the
# skill we're currently checking.
if (sc = @skills.select{|scs| scs.skill == skill}).blank?
# The skill was not found, so create a new entry with the SkillCategory class and set the
# correct values for the various properties
sc = SkillCategory.new
sc.labels << 'Education skills' unless sc.labels.include?('Education skills')
sc.score= 1
sc.is_educational_skill?= true
sc.skill= skill
else
# The skill was found in one of the SkillCategory objects. So we update the score by
# 1 and store the label of the current concept, unless that label is already present.
sc.labels << 'Education skills' unless sc.labels.include?('Education skills')
sc.is_educational_skill?= true
sc.score+= 1
end
end
=begin
@cv.educational_skills.all(:include => :labels).each do |skill|
unless (array = @skills.assoc skill).blank?
array[1]+= 1
array[3] << 'Education skills' unless array[3].include? 'Education skills'
else
@skills << [skill, 1, ['Education skills'], []]
end
end
=end
end
# Returns all uniq skills with their score and section found.
# array structure for each element
# - 0 : the skill object
# - 1 : the score for the skill
# - 2 : CV location of the skill
# - 3 : ESCO group of the skill
def skills
@skills
end
# Returns all uniq occupations with their score and section found.
# array structure for each element
# - 0 : the occupation object
# - 1 : the score for the occupation
# - 2 : the CV location of the occupation
# - 3 : empty array for occupations
def occupations
@occupations
end
end
#=分类程序
#本课程将根据简历中的技能和职业对简历进行分类
#简历。因为控制器最初有一大块代码,这个类将分解这些代码
#登录到单独的函数调用中,并将所有内容保存在变量中,以便于访问。
类别分类程序
#初始化CvCategorizer的新实例
def初始化cv、语言
@cv=cv
@语言
@职业=[]
@技能=[]
结束
#通过查看存储的CV并收集
#所有想要的职业和过去的职业。这些都存放在里面
#内部占用数组作为uniq列表。
职业
所有职业=@cv.所需职业.所有(:include=>:labels)+@cv.过去职业.所有(:include=>:labels)
所有职业。每个人都有职业|
oc=职业类别。新建
主管是否需要工作?=@cv.需要的职业。包括?(职业)
主管是否有工作经验?=@cv.过去的职业。包括?(职业)
oc.职业=职业
如果@职业。包括?(oc)
@职业:标签)+@cv.过去的职业。全部(:include=>:标签)
所有职业。每个人都有职业|
节=[]
第节删除oc上的问号。是否需要工作?oc.你有工作经验吗?第21行和第22行。删除oc上的问号。是否需要工作?oc.你有工作经验吗?第21行和第22行。ruby允许在方法名中使用问号,而不是变量(任何类型)或对象属性
ruby的方法是将实例方法添加到OccupationCategory类中,如下所示:
class OccupationCategory
def is_desired_work?
...
end
def is_work_experience?
...
end
end
所以你以后可以像这样使用它
oc.is_desired_work?
oc.is_work_experience?
ruby允许在方法名中使用问号,而不是变量(任何类型)或对象属性
ruby的方法是将实例方法添加到OccupationCategory类中,如下所示:
class OccupationCategory
def is_desired_work?
...
end
def is_work_experience?
...
end
end
所以你以后可以像这样使用它
oc.is_desired_work?
oc.is_work_experience?
@neko-代码中的其他位置缺少赋值运算符之间的空格。分配应始终为foo=bar
,而不是foo=bar
。使用后者会遇到麻烦。检查您的代码以了解这些。始终在赋值运算符的两侧使用空格。已更新我的代码。但为什么会这样呢?造成问题?我的意思是数组类有一个include?函数,我想要类似的thing@neko-代码中的其他位置缺少赋值运算符之间的空格。分配应始终为foo=bar
,而不是foo=bar
。使用后者会遇到麻烦。检查您的代码以了解这些。始终在赋值运算符的两侧使用空格。已更新我的代码。但为什么会这样呢?造成问题?我的意思是数组类有一个include?功能,我想要类似的东西