Hi, I’m CFC
I’m new at here.
Nice to meet you:)
Likewise and welcome to the list

I just coded an extension for Array.
It will return the longest element of an array.
I hope you posted this for comments, if not just ignore my post, you
will be in good company

Before I go into the details, one important point, ruby classes are
open which is one of the features of Ruby I like most, but be
careful with this, I use it a lot, but my rule is, never do this in a
library, or a program that might be designed for reuse, exceptions are
of course libraries which just do that, IOW libraries that are
designed and documented as an extensions of a core class.

class Array
def longest
map {|e| e.to_s }.max
end
end
I think it is not exactly what OP wanted
because %w{x aaaa}.max is “x”
so I will try to comply to his needs
def longest
map{ |e| [e.to_s.size, e.to_s]}.max.first
end

Thanks for your reply, I learned more on this thread
But I have a question:
If I have an array contain:
ary = [1, 12, 234, “456”]
there has two elements which size is 3, but the longest method just
returned
one of them.
I can’t solve it

I think you’d have to reverse your array.
Together we might get there ;), maybe just applying #last instead of#first.
See Harry’s code too, which
uses a block with max.
Which is great, I admit that is the nicest solution, learned something
again
You could also do:

Thanks for your reply, I learned more on this thread
But I have a question:
If I have an array contain:
ary = [1, 12, 234, “456”]
there has two elements which size is 3, but the longest method just returned
one of them.
I can’t solve it
Alternatively I can use my erroneous code from above to give you all
longest elements
def longest
longest_size = map{ |e| [e.to_s.size, e.to_s]}.max.first
reject{ |x| x.size < longest}
end

this is slow though, maybe the following is preferable - but not so
readable -
def longest
inject([]){ |s, e|
if s.empty? || s.first.size < e.to_s.size then [e]
elsif s.first.size == e.to_s.size then s << e
else s
end
}
end

Robert
You see things; and you say Why?
But I dream things that never were; and I say Why not?
– George Bernard Shaw

Thanks for your reply, I learned more on this thread
But I have a question:
If I have an array contain:
ary = [1, 12, 234, “456”]
there has two elements which size is 3, but the longest method just returned
one of them.
I can’t solve it

It depends how you want to break the tie. Here’s a way to do it where
a size tie is broken by string comparison, which is ASCII-based. The
nonzero? method will return -1 or 1 if the two string sizes are not
equal. If they are equal, the comparison will evaluate to zero and
nonzero? will be false – which means the second comparison (the ASCII
one) will be executed.

Thanks for your reply, I learned more on this thread
But I have a question:
If I have an array contain:
ary = [1, 12, 234, “456”]
there has two elements which size is 3, but the longest method just returned
one of them.
I can’t solve it

You get full credits for this anyway it is really beautiful, can you
stop being so clever ;).

Since I overlooked your posting I can’t be so clever, can I?

With #inject it’s really special, it took me a while to pick it up and
now I can’t stop using it. IIRC I even went to the exercise to
implement all methods in Enumerable by using #inject - maybe you can
find it in the archives.