--CQDko/0aYvuiEzgn
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Sam Sungshik Kong (ssk / chol.nospam.net) wrote:

> Hello!
> 
> While I was making my own utility functions, I met an interesting situation.
> I want to know how to solve this kind of problem.
> 
> I want to use Excel from ruby using OLE.
> To make steps simple, I created functions to open excel and close it.
> 
> get_excel function has no problem.
> I call it like
> xl = get_excel
> 
> When I'm done with it, I want to close it and unload it from memory.
> The right procedure is
> 
> xl.quit
> xl = nil
> GC.start
> 
> I want to make a function for the 3 steps.
> 
> def quit_excel(xl)
>   xl.quit
>   xl = nil
>   GC.start
> end
> 
> When I call it, I do
> 
> quit_excel(xl) #don't work as expected
> 
> As you know, the argument is call-by-value and even if I set nil to xl, the
> outer reference is still referencing Excel.
> So GC won't collect it.
> 
> How can I solve this problem?

def with_excel
  xl = get_excel
  yield xl
ensure
  xl.quit
  xl = nil
  GC.start
end

...

with_excel do |xl|
  ... # your code
end

-- 
Eric Hodel - drbrain / segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E  7C11 332A 551C 796C 9F04


--CQDko/0aYvuiEzgn
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQFAv2X4MypVHHlsnwQRAsStAKDgAxeTon25T8M6NjaahDPznBmWGQCfa72z
8ShPx8ur+KAdjNPpkzjmD7Q
Ho
-----END PGP SIGNATURE-----

--CQDko/0aYvuiEzgn--