can't sort an array

C

chen li

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



____________________________________________________________________________________
Need a quick answer? Get one in minutes from people who know.
Ask your question on www.Answers.yahoo.com
 
M

Martin DeMello

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

sort_by is your friend. I don't have a windows machine handy to
experiment, but say an Rows:WIN32OLE_METHOD defines a #name, you can
say

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

or even

m.map {|i| i.name}.sort

if what you want is the names, rather than the Rows:WIN32OLE_METHOD
objects themselves.

Of course, if you *can* print it, it evidently defines to_s and
inspect methods, so this will work:

puts m.map {|i| i.to_s}.sort

or

puts m.map {|i| i.inspect}.sort

martin
 
C

chen li

--- Martin DeMello said:
sort_by is your friend. I don't have a windows
machine handy to
experiment, but say an Rows:WIN32OLE_METHOD defines
a #name, you can
say

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

or even

m.map {|i| i.name}.sort

if what you want is the names, rather than the
Rows:WIN32OLE_METHOD
objects themselves.

Of course, if you *can* print it, it evidently
defines to_s and
inspect methods, so this will work:

puts m.map {|i| i.to_s}.sort

or

puts m.map {|i| i.inspect}.sort

martin

Thanks and all of them work. But I just don't
understand why the line code below works

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

yet p m.sort.each{|i| i.name} FAILS.

Li






____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com
 
D

dblack

Hi --

Thanks and all of them work. But I just don't
understand why the line code below works

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

yet p m.sort.each{|i| i.name} FAILS.

sort and sort_by work by comparing pairs of objects using the <=>
("spaceship") method. So in your second example, you're asking Ruby
to do this:

object1 <=> object2 # etc.

The problem is (I assume) that these objects don't have a <=> method.

However, if you compare by name, you're doing:

object1.name <=> object2.name

Since names are strings, they *do* have a <=> method, so the
comparison can take place.

You can always define <=> for your class:

class C
attr_accessor :name
def <=>(other)
self.name <=> other.name
end
end

There's another issue, though. Here:

m.sort.each{|i| i.name}

I don't think you really want each. each returns its receiver, so
that snippet is functionally equivalent to:

m.sort

If you want to grab all the names in a new array, you would use map
rather than each.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
C

chen li

Hi all,

Thank you in advance for any feedback.

I need to print out the values for Excel constants.
But the code I use just print the constant's name. The
only way I can get the values for constants is to
print them one by one. What is the Ruby way to do it?

Li

##
require 'win32ole'

class Excel_Const
end

excel=WIN32OLE.new('Excel.Application')
WIN32OLE.const_load(excel, Excel_Const)

#puts Excel_Const.constants.sort
Excel_Const.constants.sort.each{|i| puts i}

puts
puts "This is the value"
puts Excel_Const::XlFullPage

##output
CONSTANTS
Xl24HourClock
...
XlYes
XlZero
This is the value
3
Exit code: 0









____________________________________________________________________________________
Any questions? Get answers on any topic at www.Answers.yahoo.com. Try it now.
 
M

Martin DeMello

I need to print out the values for Excel constants.
But the code I use just print the constant's name. The
only way I can get the values for constants is to
print them one by one. What is the Ruby way to do it?

Use const_get - it's a method defined on Module.

Excel_Const.constants.sort.each{|i|
puts "The value of Excel_Const::#{i} is "+Excel_Const.const_get(i)
}

martin
 
C

chen li

--- Martin DeMello said:
Use const_get - it's a method defined on Module.

Excel_Const.constants.sort.each{|i|
puts "The value of Excel_Const::#{i} is
"+Excel_Const.const_get(i)
}

Thank you very much. BTW it doesn't work when + is
there so I change the code into the following:

Excel_Const.constants.sort.each do|i|
print "Excel_Const::#{i} is ","\t",
Excel_Const.const_get(i)
puts
end

Li




____________________________________________________________________________________
Any questions? Get answers on any topic at www.Answers.yahoo.com. Try it now.
 
M

Martin DeMello

Thank you very much. BTW it doesn't work when + is
there so I change the code into the following:

Oops - yes, + assumes that all the constants are strings. Should have
been +Excel_Const.const_get(i).to_s

You could also just inline it into the string: puts "The value of
Excel_Const::#{i} is #{Excel_Const.const_get(i)}" since #{}
automatically calls to_s on the return value of the block.

martin
 
C

chen li

--- Martin DeMello said:
You could also just inline it into the string:
puts "The value of
Excel_Const::#{i} is #{Excel_Const.const_get(i)}"
since #{}
automatically calls to_s on the return value of the
block.

martin

Once again thank you very much,

Li



____________________________________________________________________________________
Need a quick answer? Get one in minutes from people who know.
Ask your question on www.Answers.yahoo.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,737
Latest member
CarleyHarm

Latest Threads

Top