[英]Problems with Ruby one? and include? methods
MCVE (Minimal, Complete, Verifiable Example) in order to reproduce the same problem as mine: MCVE(最小,完整,可验证的示例)为了重现与我相同的问题:
at rspec file place:在 rspec 文件位置:
RSpec.describe 'Predicate Enumerable Exercises' do
describe 'coffee drink exercise' do
it 'returns true when espresso is included' do
drink_list = ["milk", "juice", "espresso"]
expect(coffee_drink?(drink_list)).to be true
end
end
describe 'valid scores exercise' do
it 'returns true when only one score is a 10' do
score_list = { easy_to_read: 10, uses_best_practices: 8, clever: 7 }
perfect_score = 10
expect(valid_scores?(score_list, perfect_score)).to be true
end
end
end
at the actual functions file:在实际功能文件中:
def coffee_drink?(drink_list)
drink_list.include?("coffee" || "espresso")
end
def valid_scores?(score_list, perfect_score)
score_list.one?{|score| score == perfect_score}
end
I am having a hard time figuring out what is wrong in my code because I am relying on built-in methods (one?, include?) so I'm not sure what is the actual problem我很难弄清楚我的代码有什么问题,因为我依赖于内置方法(一个?,包括?)所以我不确定实际问题是什么
I created 2 methods, coffee_drink?(drink_list)
and valid_scores?(score_list, perfect_score)
.我创建了 2 个方法, coffee_drink?(drink_list)
和valid_scores?(score_list, perfect_score)
。
coffee_drink?
gets an array, which then returns true only if it includes either the word coffee
or espresso
.获取一个数组,然后仅当它包含单词coffee
或espresso
时才返回 true。 I used the following to return the expected: drink_list.include?("coffee" || "espresso")
But when I have espresso
on my list and no coffee
the console returns:我使用以下内容返回预期: drink_list.include?("coffee" || "espresso")
但是当我的列表中有espresso
并且没有coffee
时,控制台会返回:
returns true when espresso is included (FAILED - 1)包含 espresso 时返回 true (FAILED - 1)
Where drink_list
is ["milk", "juice", "espresso"]
其中drink_list
是["milk", "juice", "espresso"]
For valid_scores?
对于valid_scores?
it gets an array and integer, and returns true when only one value in the array is equal to that integer.它获取一个数组和 integer,并在数组中只有一个值等于 integer 时返回 true。 But I expected that score_list.one?{|score| score == perfect_score}
但我期待score_list.one?{|score| score == perfect_score}
score_list.one?{|score| score == perfect_score}
would return true when only one value is true. score_list.one?{|score| score == perfect_score}
当只有一个值为真时会返回真。 Instead I get:相反,我得到:
returns true when only one score is a 10 (FAILED - 2)当只有一个分数是 10 时返回 true (FAILED - 2)
Where the array is数组在哪里
score_list = { easy_to_read: 10, uses_best_practices: 8, clever: 7 }
and the integer is integer 是
perfect_score = 10
I am using ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux] if that helps.如果有帮助,我正在使用 ruby 2.7.2p137(2020-10-01 修订版 5445e04352)[x86_64-linux]。
Your include?
你的include?
code behaves different than expected because:代码的行为与预期不同,因为:
"coffee" || "espresso" #=> "coffee"
# so
drink_list.include?("coffee" || "espresso")
# evaluates to
drink_list.include?("coffee")
You'll have to either use 2 separate include?
您必须使用 2 个单独include?
calls, or some other option likeany?
电话,或其他类似any?
or &
.或&
。
drink_list.include?("coffee") || drink_list.include?("espresso")
# or
drink_list.any? { |drink| drink == "coffee" || drink == "espresso" }
drink_list.any?(/\A(coffee|espresso)\z/)
(drink_list & ["coffee", "espresso"]).size.positive? # !(...).empty? also works
Your one?
你的one?
code behaves different than expected because score_list
is not an array, but a hash (dictionary/map in other languages).代码的行为与预期不同,因为score_list
不是数组,而是 hash(其他语言的字典/地图)。 A hash consists of key/value pairs which are passed as array ( [key, value]
) to the one?
A hash 由键/值对组成,它们作为数组( [key, value]
)传递给one?
block.堵塞。
score_list.one? { |pair| pair.last == perfect_score }
Alternatively you can use array decomposition :或者,您可以使用数组分解:
score_list.one? { |key, value| value == perfect_score }
Or retrieve only thevalues
beforehand:或者事先只检索values
:
score_list.values.one? { |score| score == perfect_score }
If you think this looks familiar, it's from The Odin Project's Ruby course, Predicate Enumerable Methods section and the exercise is from the predicate_enumerable_exercises.rb.如果您认为这看起来很熟悉,它来自 The Odin Project 的 Ruby 课程,谓词可枚举方法部分,练习来自 predicate_enumerable_exercises.rb。
For the first one I used an if, else statement like:对于第一个,我使用了 if,else 语句,例如:
if drink_list.include?('coffee') || drink_list.include?('espresso')
true
else
false
end
And for the last one, you can pass an argument to one?
对于最后一个,您可以将参数传递给one?
. . I did it like this:我是这样做的:
score_list.values.one?(perfect_score)
Also, you said array, I think you meant hash, score_list is a hash.另外,你说数组,我想你的意思是 hash,score_list 是 hash。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.