4 ms·
This is something that has bothered me about my Ruby usage. A lot of the time, I feel like I'm writing "dynamic" code that really just needs to be pre-processe
by johnnygood 16y ago
This is something that has bothered me about my Ruby usage. A lot of the time, I feel like I'm writing "dynamic" code that really just needs to be pre-processed.
class Cool
[:ice, :soda, :snow].each do |item|
define_method item do
#some code
end
end
end
There isn't a real reason why that couldn't be pre-processed. I don't know a ton of Java, but the idea of compile-time code generation is appealing. Does anyone know more about this?
- deleted 16y ago[deleted]
- ambition 16y agoSolving this problem in general is a major area of research. Look up "Partial Evaluation."
- grav1tas 16y agoYes! I second this. Partial evaluation is especially compelling stuff for improving efficiency. I'm glad other people on HN are into it.
- aaronblohowiak 16y agoThat code is only run once per application lifetime. Without risking DRY, I don't know what you mean by 'pre-processed'. Do you care if it is run at the parse time instead of the Class Object definition time?
- sha90 16y agoPre-processed here likely means what you think it means. DRY is not really a concept meant to be applied to method definitions (IMO), but rather method bodies-- of course there are many ways to abuse the interpretation of DRY as well. As far as whether you should care about compile vs load time-- in Ruby you don't need to, but if the language had static typing, it would make a difference.
- stephen 16y agoI think the reason is merely that compile-time metaprogramming hasn't been adopted in any mainstream programming language (yet). Boo can do cool things like that though: http://bamboo.github.com/2010/07/11/boo-meta-programming-facilities-I-the-ast.html http://bamboo.github.com/2010/07/11/boo-meta-programming-fac...