[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Can't sort array

Bernard Kenik

12/5/2006 7:49:00 PM

Hi all,

I want to print all the methods in excel OLE object
in alphabetic order but the sort method is
unavailable. Can anyone explain it and help me fix it?

Thank you,

Li
##
require 'win32ole'
excel = WIN32OLE.new('Excel.Application')
m = excel.ole_methods
p m.class
p m.sort

##screen output

> >ruby sort1.rb
>
Array
sort1.rb:6:in `sort': undefined method `<=>' for
Rows:WIN32OLE_METHOD (NoMethodError)
from sort1.rb:6

> >Exit code: 1
>
Try the following code to accomplish what you seem to want .. this
solution appeared in a previous thread

require 'win32ole'

class WIN32OLE
def list_ole_methods
method_names = ole_methods.collect { |m| m.name }
puts method_names.sort.uniq
end
end


excel = WIN32OLE.new('Excel.Application')
excel.list_ole_methods
excel.quit

3 Answers

Chris Hulan

12/5/2006 8:27:00 PM

0


Bernard Kenik wrote:
> Hi all,
>
> I want to print all the methods in excel OLE object
> in alphabetic order but the sort method is
> unavailable. Can anyone explain it and help me fix it?
>
> Thank you,
>
> Li
> ##
> require 'win32ole'
> excel = WIN32OLE.new('Excel.Application')
> m = excel.ole_methods
> p m.class
> p m.sort
....

Since you want to sort by method name, give a block to sort, i.e.:

p m.sort{|a| a.name <=> b.name}

Cheers
Chris

Li Chen

12/6/2006 12:18:00 AM

0

>
> Since you want to sort by method name, give a block to sort, i.e.:
>
> p m.sort{|a| a.name <=> b.name}


The line above doesn't work but it will work if change to the follows

p m.sort{|a,b| a.name <=> b.name}


Li

--
Posted via http://www.ruby-....

Jos Backus

12/7/2006 2:08:00 AM

0

On Wed, Dec 06, 2006 at 09:17:45AM +0900, Li Chen wrote:
> The line above doesn't work but it will work if change to the follows
>
> p m.sort{|a,b| a.name <=> b.name}

p m.sort_by {|a| a.name}

--
Jos Backus
jos at catnook.com