[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Array.which_long? ( I coded an extension for Array

Robert Dober

4/29/2007 2:16:00 PM

On 4/29/07, Billy Hsu <ruby.maillist@gmail.com> wrote:
> 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.
> Source:
>
> class Array
> def which_long?
> # Version 1.0
> # Coded by CFC < zusocfc @ gmail . com >
> # PLEASE DO NOT REMOVE THE COMMENT OF THIS FUNCTION, THANKS A LOT.
> # Usage:
> # ['a', 'ab', 'abc' 1234].which_long?
> # => 1234
> max, long, self_cpy = 0, "", []
> self.size.times{|i| self_cpy << self[i].to_s}
> self_cpy.each{|item| (max = item.size; long = item) if item.size > max }
> long
> end
> end
>
def longest
... forget it skipping to David's reply

> Usage:
> puts [1, 23, '456'].which_long?
> => 456
>
> CFC --
>


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