From: Austin Ziegler Date: 2003-04-19T10:41:51+09:00 Subject: Re: Extend initialize method in String Class On Sat, 19 Apr 2003 08:35:30 +0900, Bilbo wrote: > require 'win32ole' > > class String > def initialize (v) > @s = WIN32OLE.new('Apputility.clString') > @s.assign = v > String.initialize (v) > end > > def countOf (v) > @s.countof(v) > end > end Use Module#alias_method. class String alias_method :__initialize__, :initialize def initialize(v) @s = WIN32OLE.new('Apputility.clString') @s.assign = v __initialize__(v) end attr_reader :s end However, note that this will only work for cases where you do String#new directly; it will not work for automatically generated strings (e.g., s = "foo"). > The user programmer would code something like: > myString = "hello world" > puts myString.countOf('o') > or > myString = String.new("hello world") > puts myString.countOf('o') The user programmer doesn't need to use the Win32OLE version of countOf. You can use String#count, though: "Hello world".count("o") # => 2 -austin -- Austin Ziegler, austin@halostatue.ca on 2003.04.18 at 21:41:18