# Perplexing difference between debug and normal run of a simp

**URL:** <https://www.ruby-forum.com/t/perplexing-difference-between-debug-and-normal-run-of-a-simp/83718>\
**Category:** Ruby\
**Created:** [January 29, 2007, 6:42am UTC](https://www.ruby-forum.com/t/perplexing-difference-between-debug-and-normal-run-of-a-simp/83718 "2007-01-29T06:42:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [January 29, 2007, 6:42am UTC](https://www.ruby-forum.com/t/perplexing-difference-between-debug-and-normal-run-of-a-simp/83718/1 "2007-01-29T06:42:47Z")

</div>

Please look at this snippet -

* * *

class Test

def initialize  
@hash = {}  
end

def method\_missing(m,\*a)  
if m.to\_s =~ /=$/  
Test.class\_eval do  
define\_method($`.to_sym) do #accessor @hash[$`.to\_sym]  
end  
define\_method(m) do |arg| #mutator  
@hash[$`.to\_sym] = arg.split(",")  
end  
end  
send m, \*a  
else  
raise NoMethodError, “#{m}”  
end  
end  
end

t = Test.new  
t.a = “hello”  
puts t.a

* * *

## When I run this from the cmdline I get -

## D:\sandbox\nasir\>ruby test.rb test.rb:10:in `a': undefined method`to\_sym’ for nil:NilClass (NoMethodError) from test.rb:25

I was unable to debug this so I started the debugger session but I was  
surprised to find that it ran as expected in the debugger session ???-

* * *

D:\sandbox\nasir\>ruby -r debug test.rb  
Debug.rb  
Emacs support available.

## d:/ruby/lib/ruby/site\_ruby/1.8/ubygems.rb:10:require ‘rubygems’ (rdb:1) c hello

First of all can anyone please explain why it runs with debugger while  
fails  
with normal interpreter?

Second what is wrong with the snippet?

Thanks

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [January 29, 2007, 7:47am UTC](https://www.ruby-forum.com/t/perplexing-difference-between-debug-and-normal-run-of-a-simp/83718/2 "2007-01-29T07:47:53Z")

</div>

On Mon, 2007-01-29 at 14:41 +0900, Nasir K. wrote:

> ```
> if m.to_s =~ /=$/
> raise NoMethodError, "#{m}"
> end
> 
> ```
> 
> end  
> end
> 
> t = Test.new  
> t.a = “hello”  
> puts t.a

[snip]

> First of all can anyone please explain why it runs with debugger while fails  
> with normal interpreter?
> 
> Second what is wrong with the snippet?

Perhaps you already know this, but using a local variable in place of $`  
fixes your code:

class Test

def initialize  
@hash = {}  
end

def method\_missing(m,\*a)  
if m.to\_s =~ /=$/  
key = $` #added here  
Test.class\_eval do  
define\_method(key.to\_sym) do #accessor  
@hash[key.to\_sym]  
end  
define\_method(m) do |arg| #mutator  
@hash[key.to\_sym] = arg.split(",")  
end  
end  
send m, \*a  
else  
raise NoMethodError, “#{m}”  
end  
end  
end

t = Test.new  
t.a = “hello,world”  
puts t.a

$` is getting clobbered when you go into Test.class\_eval so it gets set  
to nil. Why this is different to a local variable I’m not sure to be  
honest (ditto for why debug mode should be different).

As a related, follow up question, can someone explain the difference  
between the two class\_evals in the following snippet (one works, the  
other doesn’t):

“foo”.match(/oo/)

class Test  
end

Test.class\_eval do  
define\_method($`.to_sym) do puts $` + “oobared”  
end  
end  
Test.new.f

class Test  
class\_eval do  
define\_method($`.to_sym) do puts $` + “oobared”  
end  
end  
end
