gets

N

News Groups

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
 
H

Hal Fulton

News said:
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
 
C

CT

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.
 
N

News Groups

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


 
N

Nicholas Van Weerdenburg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top