読者です 読者をやめる 読者になる 読者になる

LONGEST WORD(CodeEval)

codeeval ruby

CHALLENGE DESCRIPTION:

In this challenge you need to find the longest word in a sentence. If the sentence has more than one word of the same length you should pick the first one.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following

some line with text
another line

Each line has one or more words. Each word is separated by space char.

OUTPUT SAMPLE:

Print the longest word in the following way.

some
another

My Code

#!/usr/bin/env ruby -w

def detect_longest_word(words)
  longest_word = ''
  words.each do |word|
    longest_word = word if longest_word.length < word.length
  end
  longest_word
end

ARGF.each_line do |line|
  puts detect_longest_word(line.chomp.split)
end