cheesenthusiast
cheesenthusiast

Reputation: 139

Finding length of all strings in an array in Ruby

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.

Upvotes: 0

Views: 447

Answers (1)

Ry-
Ry-

Reputation: 225164

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

Upvotes: 1

Related Questions