4

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
bvanderw
  • 1,065
  • 2
  • 12
  • 20

2 Answers2

7

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
  • 446
  • 2
  • 4
  • 1
    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 Mar 01 '09 at 14:45
  • @MikeWoodhouse: The code came from [this link](http://rubyonwindows.blogspot.com/2009/02/win32ole-objects-class-names-and.html). I'd personally do `ole_methods.collect(&:to_s)` (ie no bang, and use Symbol#to_proc unless you're on Ruby 1.8.6). – Andrew Grimm Oct 27 '11 at 01:10
-3
  1. obj.class

  2. :

    if obj.respond_to?(methodName)
        #do your work
    end
    
chown
  • 51,908
  • 16
  • 134
  • 170
aivarsak
  • 279
  • 1
  • 6