ruby each_line comparison only returns last string -
code:
class comparer words = "asdf-asdf-e-e-a-dsf-bvc-onetwothreefourfive-bob-john" foundwords = [] file.foreach('words.txt') |line| substr = "#{line}" if words.include? substr puts "found " + substr foundwords << substr end end wordlist = foundwords.join("\n").to_s puts "words found: " + wordlist end
words.txt:
one blah-blah-blah 123-5342-123123 onetwo onetwothree onetwothreefour
i'd code return instances of include?, when code run, wordlist contains last line of words.txt ("onetwothreefour".) why don't other lines in words.txt factored?
because other lines expect found, have "hidden" newline character @ end. can see yourself.
file.foreach('words.txt') |line| puts line.inspect # or p line end
you can rid of newlines using chomp!
method on line
.
file.foreach('words.txt') |line| line.chomp! # proceed logic end
Comments
Post a Comment