I
I
IlyaMalyukov2020-10-05 23:11:05
ruby
IlyaMalyukov, 2020-10-05 23:11:05

Why is there an error when calling the map method?

def find_adjective(string)
    words = string.split(" ")
    index = words.find_index("is")
    words[index + 1]
end

lines = []
File.open("reviews.txt") do |review_file|
    lines = review_file.readlines
end

relevant_lines = lines.find_all {|line| line.include?("Truncated")}
reviews = relevant_lines.reject {|line| line.include?("--")}

adjectives = reviews.map do |review|
    adjective = find_adjective(review)
    "'#{adjective.capitalize}'"
end

puts "The critics agree, Truncated is:"
puts adjectives


`find_adjective': undefined method `+' for nil:NilClass (NoMethodError

)
error in
console

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GeneAYak, 2020-10-05
@GeneAYak

Most likely there is no line that contains is, so index is nil, and trying to add one to it causes an error

A
AVKor, 2020-10-05
@AVKor

words = %w[apple banana]
words.find_index('is').class #=> NilClass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question