[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Failed Metaprogramming PLUS Stack Level Too Deep

Ari Brown

9/14/2007 11:25:00 AM

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


3 Answers

Jano Svitok

9/14/2007 2:06:00 PM

0

On 9/14/07, Ari Brown <ari@aribrown.com> 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

Marcin Raczkowski

9/14/2007 2:21:00 PM

0

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

Ari Brown

9/14/2007 9:36:00 PM

0


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

> On 9/14/07, Ari Brown <ari@aribrown.com> 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.