Failed Metaprogramming PLUS Stack Level Too Deep

A

Ari Brown

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
 
M

Marcin Raczkowski

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
 
A

Ari Brown

- @chain = #{name}
+ @chain = "#{name}"

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top