[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

extension in c, close file when script ends, rb_io_fptr_finalize

Alexander Fleck

3/8/2007 7:52:00 AM

hi,
I wrote an extension that uses a file to handle an external device. The
initialize method opens the file and it then is permanently open. I don' t
want to open and close it every time I write to it.

I wondered how to finalize the object or to close the file when my script
ends. Currently the file keeps open and I have an open unused device.

I stumbled over the function 'rb_io_fptr_finalize', but I didn' t find a
real reference which tells me if that' s what I searched for or not.

Can someone help me or give me further infos?

thanks,
Alex.

4 Answers

Brian Candler

3/8/2007 9:19:00 AM

0

On Thu, Mar 08, 2007 at 04:55:11PM +0900, Alexander Fleck wrote:
> I wrote an extension that uses a file to handle an external device. The
> initialize method opens the file and it then is permanently open. I don' t
> want to open and close it every time I write to it.
>
> I wondered how to finalize the object or to close the file when my script
> ends. Currently the file keeps open and I have an open unused device.

I'd recommend you copy the way that Ruby's File class does it.

(1) Provide a 'close' method on your object, for the user to be able to
explicitly close it when needed.

(2) Provide a block-based method, which does an open / yield / close
sequence

File.open("/etc/passwd") do |f|
puts f.read
end
# file is now closed at this point

(preferred implementation is to use begin ... ensure .. end, so that the
close is called even if an exception occurs when the yield is called)

(3) Let the files close themselves when the program exits.

> I stumbled over the function 'rb_io_fptr_finalize', but I didn' t find a
> real reference which tells me if that' s what I searched for or not.

As I understand it, finalisers are called when the object is
garbage-collected. However there's not guarantee that an object will be
garbage-collected at any time before the program exits (and not even then,
if the user calls 'exit!')

However, if it's just a file, the operating system will close the file when
the program exits anyway.

B.

Mason Barge

7/3/2012 2:48:00 PM

0

On Mon, 2 Jul 2012 21:51:12 +0000 (UTC), "Adam H. Kerman" <ahk@chinet.com>
wrote:

>BTR1701 <atropos@mac.com> wrote:
>>"Barb May" <barbmay@nonofyourbusinessx.tv> wrote:
>>>Neal Boortz wrote:
>
>>>>Check out the pathetic attempts of Democrats over the
>>>>last few days to mitigate this idea that the individual mandate is a
>>>>tax.
>
>>>http://blogs.wsj.com/washwire/2012/07/02/romney-adviser-health-mandate-is-...
>
>>>Romney Adviser: Health Mandate Is Not a Tax
>>>It turns out Mitt Romney and President Barack Obama can agree on at
>>>least one aspect of the new health-care law: The penalty Mr. Obama
>>>imposes on people who forego insurance coverage should not be defined as
>>>a tax.
>
>>Doesn't matter what either Romney or Obama think. The Supreme Court has
>>ruled it's a tax, so it's a tax. Obama and the Dems can spin until they
>>puke and it won't change that fact.
>
>Thank you, "Barb May" lite.
>
>Actually, the penalty was ruled to be constitutional as Congress's
>tax power, but not a tax with respect to Anti-Injunction Act.
>
>Seriously, it's-a-tax-but-it's-not-a-tax is more evidence that all
>lawyers should be shot.

Uh-huh. And who's going to run the legal system? You do approve of an
independent judiciary, I assume.

Adam H. Kerman

7/3/2012 3:34:00 PM

0

Mason Barge <masonbarge@gmail.com> wrote:
>"Adam H. Kerman" <ahk@chinet.com> wrote:
>>BTR1701 <atropos@mac.com> wrote:
>>>"Barb May" <barbmay@nonofyourbusinessx.tv> wrote:
>>>>Neal Boortz wrote:

>>>>>Check out the pathetic attempts of Democrats over the
>>>>>last few days to mitigate this idea that the individual mandate is a
>>>>>tax.

>>>>http://blogs.wsj.com/washwire/2012/07/02/romney-adviser-health-mandate-is-...

>>>>Romney Adviser: Health Mandate Is Not a Tax
>>>>It turns out Mitt Romney and President Barack Obama can agree on at
>>>>least one aspect of the new health-care law: The penalty Mr. Obama
>>>>imposes on people who forego insurance coverage should not be defined as
>>>>a tax.

>>>Doesn't matter what either Romney or Obama think. The Supreme Court has
>>>ruled it's a tax, so it's a tax. Obama and the Dems can spin until they
>>>puke and it won't change that fact.

>>Thank you, "Barb May" lite.

>>Actually, the penalty was ruled to be constitutional as Congress's
>>tax power, but not a tax with respect to Anti-Injunction Act.

>>Seriously, it's-a-tax-but-it's-not-a-tax is more evidence that all
>>lawyers should be shot.

>Uh-huh. And who's going to run the legal system? You do approve of an
>independent judiciary, I assume.

What answer would I give to obtain the desired Shakespearian outcome?
There's no common sense here, Mason.

Bill Steele

7/3/2012 5:45:00 PM

0


>
> What answer would I give to obtain the desired Shakespearian outcome?
> There's no common sense here, Mason.

If there were, we would be discussing television.