感謝在Ruby-talk上的:
原本的程式碼太長,而且使用內建的功能組合起來就好
再者,原本的程式會把陣列的元素強制轉型為String
新的程式碼為:
Chris Carter:)
David A. Black
Harry
Robert Dober
James Edward
原本的程式碼太長,而且使用內建的功能組合起來就好
再者,原本的程式會把陣列的元素強制轉型為String
新的程式碼為:
class Array這個程式是由Harry所寫出的,底下轉貼原文:
def longest
# Harry < http://www.kakueki.com/ruby/list.html>
self.select{|r| r.to_s.size == self.max{|x, y| x.to_s.size <=> y.to_s.size}.to_s.size}
end
end
On 4/29/07, Billy Hsu <ruby.maillist@gmail.com> wrote:
> Thanks for your reply, I learned more on this thread :P
> 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 :(
>
Is this what you are looking for?
Do you want all longest elements?
big = [1, 12, 234,45,978, "456"].max {|x,y| x.to_s.size <=> y.to_s.size}
p [1, 12, 234,45,978, "456"].select {|r| r.to_s.size == big.to_s.size}
由於Harry不喜歡被張貼信箱,因此我將他的網站給貼上來:
也謝謝其他人,讓我學到許多東西:D
Thanks again and again!!
CFC --
http://www.kakueki.com/ruby/list.html再一次謝謝Harry的幫助:)
A Look into Japanese Ruby List in English
也謝謝其他人,讓我學到許多東西:D
Thanks again and again!!
CFC --