意外的语法错误
给出来自我的自定义模型的以下代码:
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
该代码创建 CvCategorizer 类的新实例并按顺序调用三个准备方法。他们都用从数据库检索的数据做一些时髦的事情。代码如下所示:
# = 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 [email protected]?(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
当浏览到应用程序中的相关视图时,我从服务器收到以下错误消息:
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:21: 语法错误,意外的 tIVAR,期望 kEND oc.is_desired_work?= @cv.desired_occupations.include?(... ^ /home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:22: 语法错误,意外的 tIVAR,期望 kEND ...
oc.is_work_experience?= @cv.past_occupations.include?(occ... ^ /home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:69: 语法错误,意外的“=”,需要 kEND sc.is_职业_技能? = 真 ^ /home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:75: 语法错误,意外的 kTRUE,期望 kEND /home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:108: 语法错误,意外的 kTRUE,期望 kEND /home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:114: 语法错误,意外的 kTRUE,期望 kEND
似乎我在 CvCategorize 类中丢失了一些东西,但我找不到丢失的东西。 IDE 没有显示诸如缺失末端或任何其他错误。
given the following code from my custom model:
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
The code creates a new instance of the CvCategorizer class and calls the three prepare methods in sequence. They all do funky stuff with data retrieved from the database. The code looks as follows:
# = 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 [email protected]?(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
When browsing to the relevant view in the application, i'm receiving the following error message from the server:
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:21:
syntax error, unexpected tIVAR, expecting kEND
oc.is_desired_work?= @cv.desired_occupations.include?(...
^
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:22:
syntax error, unexpected tIVAR, expecting kEND ...
oc.is_work_experience?= @cv.past_occupations.include?(occ...
^
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:69:
syntax error, unexpected '=', expecting kEND
sc.is_occupation_skill? = true
^
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:75:
syntax error, unexpected kTRUE, expecting kEND
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:108:
syntax error, unexpected kTRUE, expecting kEND
/home/arne.de.herdt/RubymineProjects/ESCO/app/models/cv_categorizer.rb:114:
syntax error, unexpected kTRUE, expecting kEND
It seems I'm missing something in the CvCategorize class, but I can't find the stuff missing. The IDE is not showing errors such as missing ends or anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
删除 oc.is_desired_work 上的问号吗?和 oc.is_work_experience?第 21 行和第 22 行。
Remove the question marks on oc.is_desired_work? and oc.is_work_experience? on lines 21 and 22.
ruby 允许在方法名称中使用问号,但不允许在变量(任何类型)或对象属性中使用问号。
ruby 方法是将实例方法添加到您的 OccupationCategory 类中,如下所示:
这样您以后就可以像这样使用它
ruby allows the question marks in method names, not the variables ( of any kind ) or object attributes.
the ruby way would be to add instance methods to your OccupationCategory class, like so:
so you could later use it like