Kernel.const_get and gets

R

Rasputin

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:in `echo'
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.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top