0
dictionary = File.foreach('dictionary.txt').map { |line| line.split('\n') }
dictionary.each{|word|
    puts word.length
        if word.length == 5
            puts word
        end
}

It says the value for each |word| is only 1. Anyone have a clue why? Thanks.

1
  • 2
    map { ... split('\n') } should return array of arrays, each of subarrays with only one element (as the delimiter \n is at the end). You want line.strip, not line.split. Voting to close as typo. Commented Aug 12, 2015 at 4:20

1 Answer 1

1

You’re mapping a function that splits a line into lines to every line. Each line will contain one line, resulting in a one-element array. Maybe you meant line.chomp?

dictionary = File.foreach('dictionary.txt').map &:chomp
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.