I'm a new member

Hi evrybody!
I’m a new member of forum ruby .
I meet with serious difficulties
Can you help me?
I have project write for compare different C++ with ruby (EX: Class
String )
thanks.

Que1bbb3nh Tre1baa7n wrote:

Hi evrybody!
I’m a new member of forum ruby .
I meet with serious difficulties
Can you help me?

Certainly. Please state the problem.

On Dec 2, 2006, at 23:49 , Quỳnh Trần wrote:

Can you help me?
I have project write for compare different C++ with ruby (EX: Class
String )

I don’t know about C++ strings, what are they like?


Eric H. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!

Hi all!

Li Chen wrote:

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,

sorry
could you like to make yourself clear !
thanks.

Eric H. wrote:

On Dec 2, 2006, at 23:49 , Quỳnh Trần wrote:

Can you help me?
I have project write for compare different C++ with ruby (EX: Class
String )

I don’t know about C++ strings, what are they like?

That depends. If the OP is describing ordinary C++ strings (not the
string
class), they are like a traditional C string, e.g. a series of
characters
terminated with a zero byte. If the OP is referring to ANSI strings,
this
page may help describe it:

http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

Unfortunately the original post doesn’t say clearly which “string” is
meant.

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

chen li wrote:

require ‘win32ole’
from sort1.rb:6

Exit code: 1

One solution is to convert the entire OLE data array into an equivalent
Ruby
type, such as an array of Ruby strings, and sort those. In this case,
you
could take the “excel.ole_methods” list and insert it into a Ruby array,
hopefully as strings, and sort the resulting array.

Quỳnh Trần wrote:

sorry
could you like to make yourself clear !
thanks.

That was a thread hijack. Either there’s a problem with one of the
gateways that makes new threads appear as replies to old ones, or Some
People Don’t Know Better ™.

Either way, it wasn’t supposed to be an answer to your problem.

David V.

— Martin DeMello [email protected] wrote:

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

On 12/3/06, chen li [email protected] wrote:

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

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

Hi –

On Sun, 3 Dec 2006, chen li wrote:

inspect methods, so this will work:
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

— Martin DeMello [email protected] wrote:

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

On 12/3/06, chen li [email protected] wrote:

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

On 12/3/06, chen li [email protected] wrote:

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

— Martin DeMello [email protected] wrote:

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