views:

839

answers:

2

I am using Ruby 1.8. Using the WIN32OLE module -

1) How do I determine the class name of an OLE object instance? 2) How can I tell whether an object instance supports a particular method?

In an Outlook automation script, I am trying to delete the items in the 'Deleted Items' folder which are older than 21 days. For mail items, I want to use the ReceivedTime property, but in order to do that, I need to check whether the item is actually a MailItem instance.

For the second, the best I have been able to come up with is (really slow):

def MethodExists(obj, methodName)
  obj.ole_methods.each{|method|
    if (method.name == methodName)
      return true
    end
  }
  return false
end
A: 

1)

obj.class

2)

if obj.respond_to?(methodName)
  #do your work
end
aivarsak
1) WIN32OLE - no use2) doesn't work with ole_methods
Mike Woodhouse
+5  A: 

With specific regard to WIN32OLE objects...

How do I determine the class name of an OLE object instance?

object.ole_obj_help.name

How can I tell whether an object instance supports a particular method?

object.ole_methods.collect!{ |x| x.to_s }.include?( 'MethodName' )
David Mullet
I think object.ole_methods.any? { |x| x.to_s == "MethodName" } would be a bit more concise. Or #detect, that would be good too.
Mike Woodhouse