英文:
How to select for multiple conditions to return one object that matches those conditions
问题
我正在创建一个测试,并试图创建一个方法,当匹配职位标题、职位类型和描述时,可以提取特定的职位ID,以防有多个具有相同标题的职位。
我无法使select
语句从实例变量中提取职位ID。调试显示,确实在实例变量中嵌套了ID,但因为我没有正确操作条件,所以条件没有被满足。
@job_posting
是包含我需要的ID的实例变量,但我需要我的参数在select
中匹配,以便随后返回ID。
当我只使用职位标题时,例如:
target_postings = @job_postings.select { |posting| posting[:posting_title] == posting_title }
它可以工作并返回我需要的ID,然而我不能这样做:
def get_specific_posting_id_for_posting(posting_title, job_type, description)
expect(@job_postings.length > 0)
target_postings = @job_postings.select { |posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description }
expect(target_postings.length == 1)
target_posting = target_postings[0]
posting_id = target_posting[:posting_id]
posting_id
end
英文:
I am creating a test, and am trying to create a method that pulls a specific job posting ID when it matches the job posting title, the job type and the description, just in case that the case that there are more than one job posting with the same title.
I cannot get the select
statement to pull the job posting ID out of the instance variable. Debugging shows that there is indeed the ID nested in the instance variable, but my conditions aren't being met because I am not doing it correctly.
@job_posting
is the instance variable that contains the ID that I need, but I need my parameters in select to match so I can subsequently return the ID.
whenever I ONLY use posting title,such as:
target_postings = @job_postings.select{|posting|posting[:posting_title]}
it works and returns the ID I need, however I cannot do this:
def get_specific_posting_id_for_posting(posting_title, job_type, description)
expect(@job_postings.length > 0)
target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
expect(target_postings.length == 1)
target_posting = target_postings[0]
posting_id = target_posting[:posting_id]
posting_id
end
答案1
得分: 4
It looks like
target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
should probably be
target_postings = @job_postings.select do |posting|
posting[:posting_title] == posting_title
&& posting[:job_type] == job_type
&& posting[:description] == description
end
Your version has three separate checks, the first two of which do nothing, only the last statement in the block is actually being used to determine whether the item matches.
As an aside, since it looks like you only want the single first element that matches your conditions, you might want to consider using find
instead of select
. It works the same except it will stop iterating and return as soon as it finds the first matching item.
英文:
It looks like
target_postings = @job_postings.select {|posting| posting[:posting_title] == posting_title; posting[:job_type] == job_type; posting[:description] == description}
should probably be
target_postings = @job_postings.select do |posting|
posting[:posting_title] == posting_title
&& posting[:job_type] == job_type
&& posting[:description] == description
end
Your version has three separate checks, the first two of which do nothing, only the last statement in the block is actually being used to determine whether the item matches.
As an aside, since it looks like you only want the single first element that matches your conditions, you might want to consider using find
instead of select
. It works the same except it will stop iterating and return as soon as it finds the first matching item.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论