Kernel.const_get and gets

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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: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.
    --
    Gee, Toto, I don't think we are in Kansas anymore.
    Rasputin :: Jack of All Trades - Master of Nuns

    Rasputin Guest

  2. Similar Questions and Discussions

    1. Kernel
      Just wondering, what changes, if any have been made to the OS kernel? Is it still explorer.exe? Is the browser still highly integrated? Has it...
    2. kernel-source versus kernel-patch
      Hi, The description of the kernel-source-2.4.21 package says "Linux kernel source for version 2.4.21 with Debian patches". But, there is another...
    3. kernel 2.2.x or 2.4.x ?
      On Sunday 06 July 2003 22:12, Shango Oluwa wrote: Mostly because the 2.4 series had some serious (as in "don't use for production machines")...
    4. How to apply no-debianized kernel patch to debianized kernel-source?
      Hello everybody I (like many others in the last few days) have a problem with kernel 2.4.21. I have downloaded the kernel-source-2.4.21 deb, and...
    5. Kernel 2.4.20
      Hi all. I downloaded the file: kernel-source-2.4.20_2.4.20-8_all.deb then ran dpkg -i kernel-source-2.4.20_2.4.20-8_all.deb Ran dselect, and...
  3. #2

    Default Re: Kernel.const_get and gets

    Odd. A workaround is to explicitly name the receiver of `gets`:

    class Rev
    def echo
    puts "#{self.class} would like a string:"
    puts $stdin.gets.reverse
    end
    end
    Jay Adkisson is offline Junior Member
    Join Date
    Dec 2010
    Posts
    1

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139