Kernel.const_get and gets

Well, rubberducking has finally failed me.

Using Kernel.const_get() to dynamically create instances seems
to screw up their idea of STDIN. I’m probably missing something
obvious, please can someone enlighten me?

-Program:-----------------------------
1rasputin@lb:roxo$ cat confused.rb
#! /usr/pkg/bin/ruby -w

class Echo
def echo
puts "#{self.class} would like a string:"
puts gets
end
end

class Rev
def echo
puts "#{self.class} would like a string:"
puts gets.reverse
end
end

    oldschool = [ Rev.new, Echo.new ]

    dynamic = []
    ARGV.each { |c| dynamic << Kernel.const_get(c).new }

    oldschool.each { |i| i.echo }
    dynamic.each { |i| i.echo }

1rasputin@lb:roxo$

···

-Sample output:-----------------------
0rasputin@lb:roxo$ ./confused.rb
Rev would like a string:
teststr

rtstset
Echo would like a string:
wibble
wibble
0rasputin@lb:roxo$ ./confused.rb Rev Echo
Rev would like a string:
./confused.rb:13:in gets': No such file or directory - "Rev" (Errno::ENOENT) from ./confused.rb:13:inecho’
from ./confused.rb:22
from ./confused.rb:22:in `each’
from ./confused.rb:22

1rasputin@lb:roxo$ ruby -v
ruby 1.6.8 (2002-12-24) [i386-netbsdelf]

Todays CVS gives the same error.

Baffled is an understatement. This is part of a much larger program,
so it’s taken me a little while to narrow it down.

Gee, Toto, I don’t think we are in Kansas anymore.
Rasputin :: Jack of All Trades - Master of Nuns

Using Kernel.const_get() to dynamically create instances seems
to screw up their idea of STDIN. I'm probably missing something
obvious, please can someone enlighten me?

it has nothing to do with const_get, remove the line

   ARGV.each { |c| dynamic << Kernel.const_get(c).new }

and you'll see that you have the same error

                puts gets

                        ^^^^

the problem is here

svg% ri Kernel::gets
----------------------------------------------------------- Kernel::gets
     gets( aString=$/ ) -> aString 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...

svg%

If you give it arguments, it will try to read from these files

You want IO#gets rather than Kernel::gets

Guy Decoux

Using Kernel.const_get() to dynamically create instances seems
to screw up their idea of STDIN. I’m probably missing something
obvious, please can someone enlighten me?

it has nothing to do with const_get, remove the line

ARGV.each { |c| dynamic << Kernel.const_get(c).new }

and you’ll see that you have the same error

            puts gets
                    ^^^^

the problem is here

svg% ri Kernel::gets
----------------------------------------------------------- Kernel::gets
gets( aString=$/ ) → aString 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
 ^^^^

KERRRR-CHUNK. (sound of very large penny dropping).
Thank you very much indeed.

···

svg%

If you give it arguments, it will try to read from these files

You want IO#gets rather than Kernel::gets


Nobody can be exactly like me. Sometimes even I have trouble doing it.
– Tallulah Bankhead
Rasputin :: Jack of All Trades - Master of Nuns