Dave Burt
3/25/2006 2:21:00 PM
Hi!
Masaki Suketa wrote:
> I have a question about Win32OLE.
> I think that I'll change the behavior of WIN32OLE#[] and WIN32OLE#[]=
> in Ruby 1.9 or later.
>
> I have not commited the change yet.
> Before commiting it, I want suggestions or opinions from Win32OLE users.
No longer allowed:
> excel["Visible"] = true
> record ["StringData", 1] = 'dddd'
New:
> worksheet.cells[1,2] = 10
> env["FOO"] = "BARBAZ"
> puts rs["id"].value # This is new feature!
Excellent! This makes a lot more sense than the existing function of [] and
[]=.
When I first used WIN32OLE, I was surprised that you couldn't use [] to
access index operations.
I haven't used obj[prop, n] before, that I can recall. I think neither that
nor setproperty() are the ideal Ruby syntax for this operation, but [] seems
better. How about this:
> record ["StringData", 1] = 'dddd'
> record.setproperty("StringData", 1, 'dddd')
record[:StringData][1] = 'dddd'
Possible implementation:
class WIN32OLE
class Property
attr_reader :object, :name
def initialize(obj, name)
unless obj.kind_of?(WIN32OLE)
raise TypeError.new("cannot convert #{obj.inspect} to
WIN32OLE")
end
self.object = obj
self.name = name.to_s
end
def [](*args)
object.getproperty(name, *args)
end
def []=(*args)
object.setproperty(name, *args)
end
end
def [](*args)
if args.size == 1 && args[0].kind_of?(Symbol)
return Property.new(self, *args)
end
# everything else
end
end
(I realize there's 'win32ole/property' already, but I don't know exactly
what its purpose is.)
Thanks a lot for WIN32OLE, Masaki Suketa!
Cheers,
Dave