Daniel Finnie
1/16/2007 12:10:00 AM
That is the classic searching algorithm (almost). However there is
something more idiomatic.
Here's an example to find the biggest number:
[1, 2, 3].max {|a, b| a <=> b }
Try to adapt the array contents in that and the block to find the
longest string.
Dan
David Madden wrote:
>
>> 2. Write a function to find the longest string in an array of strings.
>>
> Is this from the Brian Schroder Ruby Course PDF?
>
> Here is my function (no doubt a better on exists):
>
> def longest(a)
> word = ''
> a.each do |i|
> if word.length < i.length
> word = i
> end
> end
> word
> end
>
>
> a = ['a', 'fish', 'is', 'messy', 'dog']
>
> puts longest(a)
>
>
>
>
>