Failed Metaprogramming PLUS Stack Level Too Deep

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

On 9/14/07, Ari B. [email protected] wrote:

   def #{name} &b
  •      @chain = #{name}
    
  •     @chain = "#{name}"
    

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

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

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

On 9/14/07, Ari B. [email protected] 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 :slight_smile:

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