# Failed Metaprogramming PLUS Stack Level Too Deep

**URL:** <https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783>\
**Category:** Ruby\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783 "2007-09-17T13:25:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783/1 "2007-09-17T13:25:42Z")

</div>

Hey all

I’m trying to dynamically define a method which will do the following.

Example:  
def INPUT(&b)  
@chain = “INPUT”  
@file \<\< " # BEGIN rules for chain _INPUT_\n"  
instance\_eval(&b)  
@file \<\< " # END rules for chain _INPUT_\n"  
@chain = nil  
end

So my plan is that I call  
chain “TEST\_CHAIN”  
and it will create a dynamic method as such.

However, due to my failure at life, liberty, and metaprogramming, I  
have to run through two commands in order to do this:

def chain(name)  
Firewall.real\_chain(name)  
$stderr.puts “Generating chain #{name} in table #{@table}” if  
@debug  
@file \<\< “iptables -t #{@table} -N #{name} # Adding chain #  
{name}\n”  
end

def Firewall.real\_chain(\*names)  
names.each do |name|  
module\_eval \<\<-“end\_eval”

```
   def #{name} &b
     @chain = #{name}
     @file << " # BEGIN rules for chain _#{name.upcase}_\n"
     instance_eval(&b)
     @file << " # END rules for chain _#{name.upcase}_\n"
     @chain = nil
   end

   end_eval
 end

```

If I have module\_eval in chain(), then I get an undefined method  
error. But here… With this setup, I get a stack level too deep by  
calling:

TEST\_CHAIN do  
puts 5  
end

Or with any other block, for that matter.

Bwah? My brain hurts!  
-------------------------------------------------------|  
~ Ari  
crap my sig won’t fit

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783/2 "2007-09-25T23:04:05Z")

</div>

On 9/14/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> def #{name} &b
> 
> ```

- 

```
     @chain = #{name}

```

- 

```
    @chain = "#{name}"

```

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783/3 "2007-09-25T23:10:32Z")

</div>

stop metaprogramming 🙂  
seriously metaprograming is nice and cool but usually leads to mistakes  
if you are not expirienced programmer and don’t have good imagination 🙂

try adding () here:  
def #{name} &b =\> def #{name}(&b)

and you forgot to add “” here:  
@chain = #{name}

should be:  
@chain = ‘#{name}’

way you did it you made something like:

def raz &b  
@chain = raz  
…

so method tried to run itself inifinitelly

next time try to “puts” or “p” your method before evaling it

---

<div class="post-metadata">

**Author:** ![Ari\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a698b9/32.png) [@Ari\_B](https://www.ruby-forum.com/u/Ari_B)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/failed-metaprogramming-plus-stack-level-too-deep/114783/4 "2007-09-25T23:10:36Z")

</div>

On Sep 14, 2007, at 10:05 AM, Jano S. wrote:

> On 9/14/07, Ari B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > def #{name} &b
> > 
> > ```
> 
> - 
> 
> ```
> @chain = #{name}
> 
> ```
> 
> - 
> 
> ```
> @chain = "#{name}"
> 
> ```
> 
> > ```
> > @file << " # BEGIN rules for chain _#{name.upcase}_\n"
> > instance_eval(&b)
> > @file << " # END rules for chain _#{name.upcase}_\n"
> > @chain = nil
> > end
> > 
> > ```

This is why I fail at life.

But the good thing (for me, at least) is that once I make this  
mistake, it will almost never happen again.

If it does, please don’t die in the mean time 🙂

Thanks a bajillion,  
~ Ari  
English is like a pseudo-random number generator - there are a  
bajillion rules to it, but nobody cares.
