Ruby interpreter bug?

I get a core dump when executing the following code.

class Bob
  ['hi', 'hello', 'howdy', 'goodbye'].each do |mname|
    instance_eval <<-EOF
      def #{mname}
        puts #{mname}
      end
    EOF
  end
end
Bob.hi

I get the same behavior when using eval instead of instance_eval and
calling "Bob.new.hi". I know that the code isn't valid, but a core
dump seems inappropriate (maybe a SyntaxError exception?). And my
Ruby interpreter is a little old.

$ ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

I'm happy to provide any further information that would be helpful.

Enjoy!

- Ryan

It works correctly for me (ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-linux])

Stefano

···

On Saturday 06 September 2008, Ryan Hinton wrote:

I get a core dump when executing the following code.

class Bob
  ['hi', 'hello', 'howdy', 'goodbye'].each do |mname|
    instance_eval <<-EOF
      def #{mname}
        puts #{mname}
      end
    EOF
  end
end
Bob.hi

I get the same behavior when using eval instead of instance_eval and
calling "Bob.new.hi". I know that the code isn't valid, but a core
dump seems inappropriate (maybe a SyntaxError exception?). And my
Ruby interpreter is a little old.

$ ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

I'm happy to provide any further information that would be helpful.

Enjoy!

- Ryan

Hi --

I get a core dump when executing the following code.

class Bob
['hi', 'hello', 'howdy', 'goodbye'].each do |mname|
   instance_eval <<-EOF
     def #{mname}
       puts #{mname}
     end
   EOF
end
end
Bob.hi

I get the same behavior when using eval instead of instance_eval and
calling "Bob.new.hi". I know that the code isn't valid, but a core
dump seems inappropriate (maybe a SyntaxError exception?). And my
Ruby interpreter is a little old.

$ ruby --version
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

It's actually not a syntax error; it's infinite recursion, because
you've got:

   def hi
     puts hi
   end

so it calls itself. So it should give you this:

(eval):2:in `hi': stack level too deep (SystemStackError)

or equivalent. Can you try it with a more recent 1.8.6?

David

···

On Sun, 7 Sep 2008, Ryan Hinton wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
   Advancing with Rails January 19-22 Fort Lauderdale, FL *
   * Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!

You have left out the quotes in << puts #{name} >>. This results in
(for example):

def hi
  puts hi
end

...which results in the function calling itself forever. On my machine
(exact same Ruby version and patchlevel, but on Windows) I get a
SystemStackError:

(eval):2:in `hi': stack level too deep (SystemStackError)
        from (eval):2:in `hi'

But it's understandable that you core dump instead.

Hope this helps.

Rico

···

On Sep 6, 9:19 pm, Ryan Hinton <iob...@email.com> wrote:

I get a core dump when executing the following code.

class Bob
['hi', 'hello', 'howdy', 'goodbye'].each do |mname|
instance_eval <<-EOF
def #{mname}
puts #{mname}
end
EOF
end
end
Bob.hi

Sorry, I was wrong. I missed the last line when copying the text. The code
doesn't work, but it doesn't give a core dump, but simply a SystemStackError
exception.

Stefano

···

On Saturday 06 September 2008, Stefano Crocco wrote:

On Saturday 06 September 2008, Ryan Hinton wrote:
> I get a core dump when executing the following code.
>
> class Bob
> ['hi', 'hello', 'howdy', 'goodbye'].each do |mname|
> instance_eval <<-EOF
> def #{mname}
> puts #{mname}
> end
> EOF
> end
> end
> Bob.hi
>
> I get the same behavior when using eval instead of instance_eval and
> calling "Bob.new.hi". I know that the code isn't valid, but a core
> dump seems inappropriate (maybe a SyntaxError exception?). And my
> Ruby interpreter is a little old.
>
> $ ruby --version
> ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]
>
> I'm happy to provide any further information that would be helpful.
>
> Enjoy!
>
> - Ryan

It works correctly for me (ruby 1.8.7 (2008-08-11 patchlevel 72)
[i686-linux])

Stefano