Li Chen wrote:
I find the method “Visible” but not"Visible=" here but the
respond_to?(“Visible”) still return false. I don’t know how to explain
This is correct. As it has been said, the respond_to? doesn’t work with
ole methods as they are served by metod_missing, which means that there
are no such methods defined for this object. A sample to illustrate
puts “Inside qwe” if m==:qwe
k.respond_to? :qwe #=> false
k.qwe ## prints: Inside qwe
So you are not able to check if there is an ole method using
respond_to?. I hoped I stated it clearly in the above post, but
Now the other problem: when you write excel.Visible=true, you call the
method Visible=, but, as already stated, respond_to? :Visible= will not
detect the presence of this method. You have to look for this method in
the ole_methods table.
In general, ole methods are divided into two groups - setters (those
that you call with = at the end, like Visible=) and other methods (those
without =, like when you call excel.Visible (it returns the current
status) or any other of similar methods. But in the ole_methods table,
they are all reported without the =, that’s why you see two elements
Visible in the table returned by excel.ole_methods. To distinguish
between them, it’s better to look up the method you are looking for in
the specific ole methods table, so: you look for setters inside
excel.ole_put_methods, and for other methods inside
excel.ole_get_methods. So, to check if there is a method called Visible
(without =, the getter), you run this code:
It returns nil if the method is not present and calling excel.Visible
will raise an error, and a non-nil value if the ole method is present.
But if you want to check if there is a setter, that is, a method named
Visible=, then you have to look it up in the other table:
Again, nil means no method, non-nil means that method Visible= is
present for the object. Note that in the last code line there is no
“Visible=”, but “Visible”, and this is because ole setters in the ole
methods table do not have the = in name, even though they are setters.
And that’s the very reason why we must look up setters and getters in
two distinct tables.
If you want, you can add a method like ole_respond_to? to the class
WIN32OLE, like this:
excel.ole_respond_to? :Visible= #=> true
Not tested carefully, so rather don’t use it unless you can understand
Hope this helps.