Gets

Hello All,

    I am new to Ruby Language. I just want to read a string using 'gets'
function, but am getting the below error message.

   ERROR MESSAGE: `gets': Bad file descriptor (Errno::EBADF)

    Can anybody please help me in solving this problem. Is it required to
require any file to use 'gets' function.

Thanks in Advance,

Thanks & Regards,
Mohan

News Groups wrote:

Hello All,

    I am new to Ruby Language. I just want to read a string using 'gets'
function, but am getting the below error message.

   ERROR MESSAGE: `gets': Bad file descriptor (Errno::EBADF)

    Can anybody please help me in solving this problem. Is it required to
require any file to use 'gets' function.

You shouldn't have any problem. You don't need a require or anything.

1. Show us the code.
2. Are you passing anything on the command line?
3. What Ruby version, what platform?

Hal

Hello All,

    I am new to Ruby Language. I just want to read a string using 'gets'
function, but am getting the below error message.

Welcome!

   ERROR MESSAGE: `gets': Bad file descriptor (Errno::EBADF)

You must be trying to run that code in the SciTE editor. Try running
the program on the command line. I remember getting this sort of an
error when I tried running a similar thing from SciTE.

    Can anybody please help me in solving this problem. Is it required to
require any file to use 'gets' function.

Thanks in Advance,

Thanks & Regards,
Mohan

Hope that helps, and no problem.

Also, here's the ri doc for gets: (try the command 'ri' to get
documentation on methods and such)

C:\Files\Ruby>ri Kernel.gets
------------------------------------------------------------ Kernel#gets
     gets(separator=$/) => string or nil

···

On Tue, 13 Jul 2004 14:47:22 +0900, News Groups <ram_naga@yahoo.com> wrote:
------------------------------------------------------------------------
     Returns (and assigns to +$_+) the next line from the list of files
     in +ARGV+ (or +$*+), or from standard input if no files are present
     on the command line. Returns +nil+ at end of file. The optional
     argument specifies the record separator. The separator is included
     with the contents of each record. A separator of +nil+ reads the
     entire contents, and a zero-length separator reads the input one
     paragraph at a time, where paragraphs are divided by two
     consecutive newlines. If multiple filenames are present in +ARGV+,
     +gets(nil)+ will read the contents one file at a time.

        ARGV << "testfile"
        print while gets

     _produces:_

        This is line one
        This is line two
        This is line three
        And so on...

     The style of programming using +$_+ as an implicit parameter is
     gradually losing favor in the Ruby community.

Hello,

    Thanks a lot for the reply,

    The problem is with SciTE editor. I tried the same program on the
command line and it is working fine.

Warm Regards,
Mohan

"CT" <demerzel@gmail.com> wrote in message
news:ecaa86390407122305580b56eb@mail.gmail.com...

> Hello All,
>
> I am new to Ruby Language. I just want to read a string using 'gets'
> function, but am getting the below error message.

Welcome!

> ERROR MESSAGE: `gets': Bad file descriptor (Errno::EBADF)

You must be trying to run that code in the SciTE editor. Try running
the program on the command line. I remember getting this sort of an
error when I tried running a similar thing from SciTE.

> Can anybody please help me in solving this problem. Is it required

to

···

On Tue, 13 Jul 2004 14:47:22 +0900, News Groups <ram_naga@yahoo.com> wrote:
> require any file to use 'gets' function.
>
> Thanks in Advance,
>
> Thanks & Regards,
> Mohan
>
>
Hope that helps, and no problem.

Also, here's the ri doc for gets: (try the command 'ri' to get
documentation on methods and such)

C:\Files\Ruby>ri Kernel.gets
------------------------------------------------------------ Kernel#gets
     gets(separator=$/) => string or nil
------------------------------------------------------------------------
     Returns (and assigns to +$_+) the next line from the list of files
     in +ARGV+ (or +$*+), or from standard input if no files are present
     on the command line. Returns +nil+ at end of file. The optional
     argument specifies the record separator. The separator is included
     with the contents of each record. A separator of +nil+ reads the
     entire contents, and a zero-length separator reads the input one
     paragraph at a time, where paragraphs are divided by two
     consecutive newlines. If multiple filenames are present in +ARGV+,
     +gets(nil)+ will read the contents one file at a time.

        ARGV << "testfile"
        print while gets

     _produces:_

        This is line one
        This is line two
        This is line three
        And so on...

     The style of programming using +$_+ as an implicit parameter is
     gradually losing favor in the Ruby community.

Sounds like Scite was the problem. Just for reference, there are two gets- Kernel#gets and IO#gets. Object includes Kernel, so you can type "gets" like a standalone function, but that's the Kernel one.

Kernel.gets is a bit tricky- if ARGV exists, it tries to open it's values as files and get the strings/lines from there. If ARGV doesn't exist, it gets it from the stdin. In which case, you can use $stdin.gets (the IO version of gets, using the global stdin variable $stdin)- assuming $stdin isn't redirected as in Scite- to continue to prompt for input from $stdin.

I ran into this today when I added command-line arguments to my program, which then promptly broke (it started trying to open the command line arguments which weren't file names).

Cool trick- run "irb", then type "global_variables" to see all global variables, or "global_variables.grep /std/" to see the ones with "std" in there names.

The cool think about Ruby is that once you are over the hump, you can keep most of it in your head. ri and irb help quickly bridge the rest.

Regards,
Nick

CT wrote:

···

On Tue, 13 Jul 2004 14:47:22 +0900, News Groups <ram_naga@yahoo.com> wrote:

Hello All,

   I am new to Ruby Language. I just want to read a string using 'gets'
function, but am getting the below error message.
   
Welcome!

  ERROR MESSAGE: `gets': Bad file descriptor (Errno::EBADF)
   
You must be trying to run that code in the SciTE editor. Try running
the program on the command line. I remember getting this sort of an
error when I tried running a similar thing from SciTE.

   Can anybody please help me in solving this problem. Is it required to
require any file to use 'gets' function.

Thanks in Advance,

Thanks & Regards,
Mohan

Hope that helps, and no problem.

Also, here's the ri doc for gets: (try the command 'ri' to get
documentation on methods and such)

C:\Files\Ruby>ri Kernel.gets
------------------------------------------------------------ Kernel#gets
    gets(separator=$/) => string or nil
------------------------------------------------------------------------
    Returns (and assigns to +$_+) the next line from the list of files
    in +ARGV+ (or +$*+), or from standard input if no files are present
    on the command line. Returns +nil+ at end of file. The optional
    argument specifies the record separator. The separator is included
    with the contents of each record. A separator of +nil+ reads the
    entire contents, and a zero-length separator reads the input one
    paragraph at a time, where paragraphs are divided by two
    consecutive newlines. If multiple filenames are present in +ARGV+,
    +gets(nil)+ will read the contents one file at a time.

       ARGV << "testfile"
       print while gets

    _produces:_

       This is line one
       This is line two
       This is line three
       And so on...

    The style of programming using +$_+ as an implicit parameter is
    gradually losing favor in the Ruby community.

Hi Mohan,

"News Groups" <ram_naga@yahoo.com> wrote in message

    The problem is with SciTE editor. I tried the same program on the
command line and it is working fine.

You can change the settings of SciTE so that this works like expected.
See:

http://www.rubygarden.org/ruby?SciTEAndStdinStdout

Warm Regards,
Mohan

HTH,
-- shanko