Writing to an open IO stream from a method

I'm having an issue figuring out how to pass an IO stream to a method.

write_it = File.new("test_ids", "w")

def getid (line, file_info)
    find_id = line.split(" ")
    find_id.each do |get_id|
        if get_id =~ /^id*/
            return get_id
        else
            file_info.puts("No ID.")
            return "No ID"
        end
    end
end

a = "name=example id=stuff"
b = "name=example noid=writefile"

getid(a, write_it)
getid(b, write_it)

I've also tried with def getid(line) thinking maybe I didn't need to
pass an IO stream, but it still doesn't work.

···

--
Posted via http://www.ruby-forum.com/.

And as soon as I post this it starts working... WTF?

I guess sometimes typing out a question just makes it work...

···

--
Posted via http://www.ruby-forum.com/.

No, it's more likely that it has to do with the fact that you do not close
the file handle properly. Better use the block form of File.open.

Cheers

robert

···

On Sun, Apr 7, 2013 at 9:46 PM, Yancy B. <lists@ruby-forum.com> wrote:

And as soon as I post this it starts working... WTF?

I guess sometimes typing out a question just makes it work...

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Robert Klemme wrote in post #1104870:

...it's more likely that it has to do with the fact that you do not close the

file handle properly.

This is not the complete code. I close the file further down, as I had
more things to write to it. The problem was that the stream was not
being sent to the method and instead I was getting an error. But it's
working now anyway, thanks.

Thanks,

- Yancy

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1104870:
>...it's more likely that it has to do with the fact that you do not close
the
file handle properly.

This is not the complete code. I close the file further down, as I had
more things to write to it.

Good. Still, the block form is superior because it ensures closing of the
stream under all conditions (even exceptions).

The problem was that the stream was not
being sent to the method and instead I was getting an error. But it's
working now anyway, thanks.

Ah, glad you found it.

Cheers

robert

···

On Mon, Apr 8, 2013 at 4:35 PM, Yancy B. <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/