[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

baffling undefined method error

Tom Cloyd

3/15/2008 1:25:00 PM

I'm getting an error I can't account for. In a method I've written I
open a file and access a yaml store:

archive_stored_list = 'archive_stored_list.yml'
archives_stored_catalog = File.open(archive_stored_list) {|i| YAML.load(i)}

I massage the data a bit, and want to write it back out to the same
file. I try to close the file so I can open it for output, but get an error:

archives_stored_catalog.close # <= "undefined method `close'" error is
written to log file here
open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)}

Why is this giving an error? Isn't the IO.close method always available?
Very confusing.

This usage is straight out of Programming Ruby 2nd edition, but it
doesn't work for me, and I can't see the error.

Would appreciate any help.

Tom

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


11 Answers

David A. Black

3/15/2008 1:31:00 PM

0

Hi --

On Sat, 15 Mar 2008, Tom Cloyd wrote:

> I'm getting an error I can't account for. In a method I've written I open a
> file and access a yaml store:
>
> archive_stored_list = 'archive_stored_list.yml'
> archives_stored_catalog = File.open(archive_stored_list) {|i| YAML.load(i)}
>
> I massage the data a bit, and want to write it back out to the same file. I
> try to close the file so I can open it for output, but get an error:
>
> archives_stored_catalog.close # <= "undefined method `close'" error is
> written to log file here
> open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)}
>
> Why is this giving an error? Isn't the IO.close method always available? Very
> confusing.
>
> This usage is straight out of Programming Ruby 2nd edition, but it doesn't
> work for me, and I can't see the error.

When you use a block with File.open, the filehandle is closed
automatically when the block exits. There's no point even saving it,
because the value of the whole operation will be nil.


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Tom Cloyd

3/15/2008 1:33:00 PM

0

Tom Cloyd wrote:
> I'm getting an error I can't account for. In a method I've written I
> open a file and access a yaml store:
>
> archive_stored_list = 'archive_stored_list.yml'
> archives_stored_catalog = File.open(archive_stored_list) {|i|
> YAML.load(i)}
>
> I massage the data a bit, and want to write it back out to the same
> file. I try to close the file so I can open it for output, but get an
> error:
>
> archives_stored_catalog.close # <= "undefined method `close'" error is
> written to log file here
> open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)}
>
> Why is this giving an error? Isn't the IO.close method always
> available? Very confusing.
>
> This usage is straight out of Programming Ruby 2nd edition, but it
> doesn't work for me, and I can't see the error.
>
> Would appreciate any help.
>
> Tom
>
If I do NOT try to close the file (and in all the examples I've found,
this isn't done - I just see an open to read, followed by another open
to write), I get a "Permission denied - archive_stored_list.yml"
message. That makes no sense. There CAN'T be a permission problem. I
just READ the file.

I await higher wisdom.

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Dave Thomas

3/15/2008 1:56:00 PM

0


On Mar 15, 2008, at 8:24 AM, Tom Cloyd wrote:

> I'm getting an error I can't account for. In a method I've written I
> open a file and access a yaml store:
>
> archive_stored_list = 'archive_stored_list.yml'
> archives_stored_catalog = File.open(archive_stored_list) {|i|
> YAML.load(i)}
>
> I massage the data a bit, and want to write it back out to the same
> file. I try to close the file so I can open it for output, but get
> an error:
>
> archives_stored_catalog.close # <= "undefined method `close'" error
> is written to log file here
> open(archive_stored_list,"w") {|i|
> YAML.dump(archives_stored_catalog,i)}
>
> Why is this giving an error? Isn't the IO.close method always
> available? Very confusing.


The block form of IO.open returns the value of the block. So, what is
the class of archives_stored_catalog? It isn't an IO object. It's your
loaded YAML data. So it doesn't have a close method.

Regards


Dave Thomas

David A. Black

3/15/2008 2:01:00 PM

0

Hi --

On Sat, 15 Mar 2008, Dave Thomas wrote:

>
> On Mar 15, 2008, at 8:24 AM, Tom Cloyd wrote:
>
>> I'm getting an error I can't account for. In a method I've written I open a
>> file and access a yaml store:
>>
>> archive_stored_list = 'archive_stored_list.yml'
>> archives_stored_catalog = File.open(archive_stored_list) {|i| YAML.load(i)}
>>
>> I massage the data a bit, and want to write it back out to the same file. I
>> try to close the file so I can open it for output, but get an error:
>>
>> archives_stored_catalog.close # <= "undefined method `close'" error is
>> written to log file here
>> open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)}
>>
>> Why is this giving an error? Isn't the IO.close method always available?
>> Very confusing.
>
>
> The block form of IO.open returns the value of the block. So, what is the
> class of archives_stored_catalog? It isn't an IO object. It's your loaded
> YAML data. So it doesn't have a close method.

Yeah, that :-) (I said nil, which was an over-generalization from a
case where the block called puts.)


David

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!

Bob Hutchison

3/15/2008 2:17:00 PM

0


On 15-Mar-08, at 9:33 AM, Tom Cloyd wrote:

> Tom Cloyd wrote:
>> I'm getting an error I can't account for. In a method I've written
>> I open a file and access a yaml store:
>>
>> archive_stored_list = 'archive_stored_list.yml'
>> archives_stored_catalog = File.open(archive_stored_list) {|i|
>> YAML.load(i)}
>>
>> I massage the data a bit, and want to write it back out to the same
>> file. I try to close the file so I can open it for output, but get
>> an error:
>>
>> archives_stored_catalog.close # <= "undefined method `close'" error
>> is written to log file here
>> open(archive_stored_list,"w") {|i|
>> YAML.dump(archives_stored_catalog,i)}
>>
>> Why is this giving an error? Isn't the IO.close method always
>> available? Very confusing.
>>
>> This usage is straight out of Programming Ruby 2nd edition, but it
>> doesn't work for me, and I can't see the error.
>>
>> Would appreciate any help.
>>
>> Tom
>>
> If I do NOT try to close the file (and in all the examples I've
> found, this isn't done - I just see an open to read, followed by
> another open to write), I get a "Permission denied -
> archive_stored_list.yml" message. That makes no sense. There CAN'T
> be a permission problem. I just READ the file.
>
> I await higher wisdom.

Don't count on it from me, but do you have write permission on the file?

Cheers,
Bob

>
> t.
>
> --
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Tom Cloyd, MS MA, LMHC
> Private practice Psychotherapist
> Bellingham, Washington, U.S.A: (360) 920-1226
> << tc@tomcloyd.com >> (email)
> << TomCloyd.com >> (website & psychotherapy weblog) <<
> sleightmind.wordpress.com >> (mental health issues weblog)
> << directpathdesign.com >> (web site design & consultation)
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>

----
Bob Hutchison -- tumblelog at http://www.recurs...
Recursive Design Inc. -- weblog at http://www.recursiv...
http://www.rec... -- works on http://www.raconteur.info/cms-for-static-con...



Tom Cloyd

3/15/2008 2:21:00 PM

0

David A. Black wrote:
> Hi --
>
> On Sat, 15 Mar 2008, Dave Thomas wrote:
>
>>
>> On Mar 15, 2008, at 8:24 AM, Tom Cloyd wrote:
>>
>>> I'm getting an error I can't account for. In a method I've written I
>>> open a file and access a yaml store:
>>>
>>> archive_stored_list = 'archive_stored_list.yml'
>>> archives_stored_catalog = File.open(archive_stored_list) {|i|
>>> YAML.load(i)}
>>>
>>> I massage the data a bit, and want to write it back out to the same
>>> file. I try to close the file so I can open it for output, but get
>>> an error:
>>>
>>> archives_stored_catalog.close # <= "undefined method `close'" error
>>> is written to log file here
>>> open(archive_stored_list,"w") {|i|
>>> YAML.dump(archives_stored_catalog,i)}
>>>
>>> Why is this giving an error? Isn't the IO.close method always
>>> available? Very confusing.
>>
>>
>> The block form of IO.open returns the value of the block. So, what is
>> the class of archives_stored_catalog? It isn't an IO object. It's
>> your loaded YAML data. So it doesn't have a close method.
>
> Yeah, that :-) (I said nil, which was an over-generalization from a
> case where the block called puts.)
>
>
> David
>
OK, thanks, that's very clarifying. I utterly missed the fact that I was
using a block open. So, once the data's loaded, the file should be
closed, and thus available for re-openning.

But it isn't. THAT was my original problem - the reason I was
(mistakenly) trying to close it - so I could open it. All this code is
in the same method. "archive_stored_list" should be something I can
open, but as I said before, upon trying I get a "Permission denied -
archive_stored_list.yml" error. I have NO idea why. If I can't open that
file, I can't dump my yaml data.

Any ideas?

t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Tom Cloyd

3/15/2008 2:22:00 PM

0

Bob Hutchison wrote:
>
> On 15-Mar-08, at 9:33 AM, Tom Cloyd wrote:
>
>> Tom Cloyd wrote:
>>> I'm getting an error I can't account for. In a method I've written I
>>> open a file and access a yaml store:
>>>
>>> archive_stored_list = 'archive_stored_list.yml'
>>> archives_stored_catalog = File.open(archive_stored_list) {|i|
>>> YAML.load(i)}
>>>
>>> I massage the data a bit, and want to write it back out to the same
>>> file. I try to close the file so I can open it for output, but get
>>> an error:
>>>
>>> archives_stored_catalog.close # <= "undefined method `close'" error
>>> is written to log file here
>>> open(archive_stored_list,"w") {|i|
>>> YAML.dump(archives_stored_catalog,i)}
>>>
>>> Why is this giving an error? Isn't the IO.close method always
>>> available? Very confusing.
>>>
>>> This usage is straight out of Programming Ruby 2nd edition, but it
>>> doesn't work for me, and I can't see the error.
>>>
>>> Would appreciate any help.
>>>
>>> Tom
>>>
>> If I do NOT try to close the file (and in all the examples I've
>> found, this isn't done - I just see an open to read, followed by
>> another open to write), I get a "Permission denied -
>> archive_stored_list.yml" message. That makes no sense. There CAN'T be
>> a permission problem. I just READ the file.
>>
>> I await higher wisdom.
>
> Don't count on it from me, but do you have write permission on the file?
>
> Cheers,
> Bob
>
Yes I sure do.

~t .

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Phillip Gawlowski

3/15/2008 2:29:00 PM

0

Tom Cloyd wrote:

> But it isn't. THAT was my original problem - the reason I was
> (mistakenly) trying to close it - so I could open it. All this code is
> in the same method. "archive_stored_list" should be something I can
> open, but as I said before, upon trying I get a "Permission denied -
> archive_stored_list.yml" error. I have NO idea why. If I can't open that
> file, I can't dump my yaml data.

Close the editor reading it? Something is blocking your write access,
and it looks like an OS "error", rather than a Ruby error.

- Phillip Gawlowski

Bob Hutchison

3/15/2008 3:48:00 PM

0


On 15-Mar-08, at 10:22 AM, Tom Cloyd wrote:

>>> If I do NOT try to close the file (and in all the examples I've
>>> found, this isn't done - I just see an open to read, followed by
>>> another open to write), I get a "Permission denied -
>>> archive_stored_list.yml" message. That makes no sense. There CAN'T
>>> be a permission problem. I just READ the file.
>>>
>>> I await higher wisdom.
>>
>> Don't count on it from me, but do you have write permission on the
>> file?
>>
>> Cheers,
>> Bob
>>
> Yes I sure do.

Can post the write code?

Cheers,
Bob

----
Bob Hutchison -- tumblelog at http://www.recurs...
Recursive Design Inc. -- weblog at http://www.recursiv...
http://www.rec... -- works on http://www.raconteur.info/cms-for-static-con...



Todd Benson

3/15/2008 10:38:00 PM

0

On Sat, Mar 15, 2008 at 8:33 AM, Tom Cloyd <tomcloyd@comcast.net> wrote:
> Tom Cloyd wrote:
> > I'm getting an error I can't account for. In a method I've written I
> > open a file and access a yaml store:
> >
> > archive_stored_list = 'archive_stored_list.yml'
> > archives_stored_catalog = File.open(archive_stored_list) {|i|
> > YAML.load(i)}
> >
> > I massage the data a bit, and want to write it back out to the same
> > file. I try to close the file so I can open it for output, but get an
> > error:
> >
> > archives_stored_catalog.close # <= "undefined method `close'" error is
> > written to log file here
> > open(archive_stored_list,"w") {|i| YAML.dump(archives_stored_catalog,i)}
> >

The File.open block version automagically closes the file.

Todd