简体   繁体   中英

Problems with Ruby one? and include? methods

MCVE (Minimal, Complete, Verifiable Example) in order to reproduce the same problem as mine:

at rspec file place:

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) .

coffee_drink? gets an array, which then returns true only if it includes either the word coffee or espresso . 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:

returns true when espresso is included (FAILED - 1)

Where drink_list is ["milk", "juice", "espresso"]

For valid_scores? it gets an array and integer, and returns true when only one value in the array is equal to that integer. But I expected that score_list.one?{|score| score == perfect_score} score_list.one?{|score| score == perfect_score} would return true when only one value is true. Instead I get:

returns true when only one score is a 10 (FAILED - 2)

Where the array is

score_list = { easy_to_read: 10, uses_best_practices: 8, clever: 7 }

and the integer is

perfect_score = 10

I am using ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux] if that helps.

Your 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? calls, or some other option likeany? 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? code behaves different than expected because score_list is not an array, but a hash (dictionary/map in other languages). A hash consists of key/value pairs which are passed as array ( [key, value] ) to the 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:

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.

For the first one I used an if, else statement like:

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? . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM