ruby 作业订单列表中的单个依赖项在其结果上重复
single dependency in ruby job order list is duplicating on its result
我有一份工作清单,并为以下问题编写了规范,其中列表中的一项依赖于另一项。我得到的结果是两次返回列表项 c。题目如下:
Given the following job structure:
a =>
b => c
c =>
The result should be a sequence that positions c before b, containing all three jobs abc.
我很困惑,因为我的测试失败如下
1) JobsList should return ordered multiple jobs with the dependency if multiple jobs with single dependency were supplied
Failure/Error: expect(JobsList.sequence({'a' => nil, 'b' => 'c', 'c' => nil})).to eq ['a', 'c', 'b']
expected: ["a", "c", "b"]
got: ["a", "c", "c", "b"]
(compared using ==)
我在这里不知所措,我做错了什么,有人可以提供任何建议吗?
jobs_list_spec
require 'jobs_list'
describe JobsList do
it "should return an empty sequence of jobs if a blank hash was supplied" do
expect(JobsList.sequence({})).to eq []
end
it "should return an single job character if a single jobs was supplied" do
expect(JobsList.sequence({'a' => nil})).to eq ['a']
end
it "should return multiple jobs characters if a multiple jobs were supplied" do
expect(JobsList.sequence({'a' => nil, 'b' => nil, 'c' => nil})).to eq ['a', 'b', 'c']
end
it "should return ordered multiple jobs with the dependency if multiple jobs with single dependency were supplied" do
expect(JobsList.sequence({'a' => nil, 'b' => 'c', 'c' => nil})).to eq ['a', 'c', 'b']
end
jobs_list.rb
class JobsList
def self.sequence(jobs={})
grouped_jobs = jobs.group_by{|k, v| v}
grouped_jobs.map do |k,v|
array = []
if k
array = array + [k]
end
array = array + v.map{|a| a[0]}
end.flatten
end
end
一些可能有帮助的建议:
- 尝试修改class的接口或将逻辑移动到class的实例而不是class方法中,因为这会使其难以使用状态。
- 其次尝试将功能拆分为具有表现力名称的微小 classes,这将帮助您更轻松地思考逻辑。
如果您想保留当前基于 class 的界面,请考虑以下内容来构建您的 class 方法:
def self.sequence(jobs)
JobList.new(jobs).sequence
end
现在,您可以将逻辑转移到实例方法中;并不是说 class 方法之间不能共享实例。但这种方法可能更容易推理出来。
如果你完全卡住了,看看我解决的 Ordered Job Kata 中的逻辑是如何分解的。
我不确定我是否遇到了你的问题,但你可以试试这段代码。
class JobsList
def self.sequence(jobs={})
jobs.to_a.flatten.uniq.compact
end
end
def self.sequence(jobs={})
puts 'hi'
grouped_jobs = jobs.group_by{|k, v| v}
grouped_jobs.map do |k,v|
array = []
if k
array = array + [k]
end
array = array + v.map{|a| a[0]}
end.flatten.uniq.compact
end
我有一份工作清单,并为以下问题编写了规范,其中列表中的一项依赖于另一项。我得到的结果是两次返回列表项 c。题目如下:
Given the following job structure:
a =>
b => c
c =>
The result should be a sequence that positions c before b, containing all three jobs abc.
我很困惑,因为我的测试失败如下
1) JobsList should return ordered multiple jobs with the dependency if multiple jobs with single dependency were supplied
Failure/Error: expect(JobsList.sequence({'a' => nil, 'b' => 'c', 'c' => nil})).to eq ['a', 'c', 'b']
expected: ["a", "c", "b"]
got: ["a", "c", "c", "b"]
(compared using ==)
我在这里不知所措,我做错了什么,有人可以提供任何建议吗?
jobs_list_spec
require 'jobs_list'
describe JobsList do
it "should return an empty sequence of jobs if a blank hash was supplied" do
expect(JobsList.sequence({})).to eq []
end
it "should return an single job character if a single jobs was supplied" do
expect(JobsList.sequence({'a' => nil})).to eq ['a']
end
it "should return multiple jobs characters if a multiple jobs were supplied" do
expect(JobsList.sequence({'a' => nil, 'b' => nil, 'c' => nil})).to eq ['a', 'b', 'c']
end
it "should return ordered multiple jobs with the dependency if multiple jobs with single dependency were supplied" do
expect(JobsList.sequence({'a' => nil, 'b' => 'c', 'c' => nil})).to eq ['a', 'c', 'b']
end
jobs_list.rb
class JobsList
def self.sequence(jobs={})
grouped_jobs = jobs.group_by{|k, v| v}
grouped_jobs.map do |k,v|
array = []
if k
array = array + [k]
end
array = array + v.map{|a| a[0]}
end.flatten
end
end
一些可能有帮助的建议:
- 尝试修改class的接口或将逻辑移动到class的实例而不是class方法中,因为这会使其难以使用状态。
- 其次尝试将功能拆分为具有表现力名称的微小 classes,这将帮助您更轻松地思考逻辑。
如果您想保留当前基于 class 的界面,请考虑以下内容来构建您的 class 方法:
def self.sequence(jobs)
JobList.new(jobs).sequence
end
现在,您可以将逻辑转移到实例方法中;并不是说 class 方法之间不能共享实例。但这种方法可能更容易推理出来。
如果你完全卡住了,看看我解决的 Ordered Job Kata 中的逻辑是如何分解的。
我不确定我是否遇到了你的问题,但你可以试试这段代码。
class JobsList
def self.sequence(jobs={})
jobs.to_a.flatten.uniq.compact
end
end
def self.sequence(jobs={})
puts 'hi'
grouped_jobs = jobs.group_by{|k, v| v}
grouped_jobs.map do |k,v|
array = []
if k
array = array + [k]
end
array = array + v.map{|a| a[0]}
end.flatten.uniq.compact
end