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

On Saturday 06 September 2008, Ryan H. wrote:

end
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, Stefano C. wrote:

end

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

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 Sep 6, 9:19 pm, Ryan H. [email protected] wrote:

end
Bob.hi

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

Hi –

On Sun, 7 Sep 2008, Ryan H. wrote:

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