P
Patrick Li
Hi,
I'm writing a DSL for some parsing. And would like the following
functionality. I was wondering if there's some metaprogramming experts
in here that can share a bit of wisdom.
Here's the ideal functionality:
string = Farm.create do
barn do
animal "dog"
animal "cat"
end
pond do
animal "whale"
animal "shark"
end
end
The string should print:
Farm contains
[
Barn contains
[
dog
cat
]
Pond contains
[
whale
shark
]
]
It would be really really nice to have this also:
Farm.create do
animal "whale"
end
#throws "IllegalMethodError: method animal() can only be called under
barn()
I currently have a rather inelegant hack using instance_eval, which
messes up a lot of other things.
Thanks a lot for your help.
-Patrick
I'm writing a DSL for some parsing. And would like the following
functionality. I was wondering if there's some metaprogramming experts
in here that can share a bit of wisdom.
Here's the ideal functionality:
string = Farm.create do
barn do
animal "dog"
animal "cat"
end
pond do
animal "whale"
animal "shark"
end
end
The string should print:
Farm contains
[
Barn contains
[
dog
cat
]
Pond contains
[
whale
shark
]
]
It would be really really nice to have this also:
Farm.create do
animal "whale"
end
#throws "IllegalMethodError: method animal() can only be called under
barn()
I currently have a rather inelegant hack using instance_eval, which
messes up a lot of other things.
Thanks a lot for your help.
-Patrick