metaprogramming

How does this deprecate method work?

I was trying to understand this call: deprecate :new_record?, :new? which uses this deprecate method: def deprecate(old_method, new_method) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{old_method}(*args, &block) warn "\#{self.class}##{old_method} is deprecated," + "use \#{self.class}##{...

Metaprogramming how much is too much?

As I have become more and more comfortable using metaprogramming techniques I have found more and more applications for metaprogramming as well. I'm working now on a little project in which I am creating classes and instances of these classes on the fly and I'm wondering if I have taken metaprogramming too far? Is there such a thing as...

alloca() of a templated array of types: how to do this?

I have a smart pointer type, and would like to construct an object that takes a pointer of that type and a count (dynamically calculated at runtime) and allocates enough memory from the stack to hold that many instances of the object the smart pointer points to. I can't seem to find quite the right syntax to achieve this; is it possible?...

template-ing a for loop in C++?

First time poster, be gentle! I have a C++ snippet below with a run-time for loop, for(int i = 0; i < I; i++) for (int j = 0; j < J; j++) A( row(i,j), column(i,j) ) = f(i,j); The snippet is called repeatedly. The loop bounds 'I' and 'J' are known at compile time (I/J are the order of 2 to 10). I would like to unroll the loop...

What are some good Xcode scripts to speed up development?

Xcode allows you to create automated scripts for performing repetitive tasks. What scripts have you written to speed up development? ...

generate mpl::vector from fusion::vector

How to generate fusion::vector from mpl::vector? How to generate mpl::vector from fusion::vector? BOOST_MPL_ASSERT((is_same< fusion::vector<int, char>, generate_fusion_vector<mpl::vector<int, char> >::type >)); BOOST_MPL_ASSERT((is_same< mpl::vector<int, char>, gen...

Design for Ruby app using meta programming

I am making a framework where objects should be created a according to a predefined XML file. For example, if in the xml file the following occurs: <type name="man"> <property name="name" type="string"> <property name="height" type="int"> <property name="age" type="int"> <property name="profession" type="string" value="...

Add methods to controllers

Hi, In a Grails application I would like to add a foo() method to all my controller classes. I know that I can do this inside a plugin's doWithDynamicMethods closure using code like: application.controllerClasses.toList()*.metaClass*.foo = { println 'foo called' } However, I don't want to create a plugin just for this purpose. Is the...

pass data from controller to filter

Hi, In a Grails application I'm looking for some way to pass data from a controller action to a filter that runs after the action. I was thinking of something like: class MyController { def myAction = { render(view:"myView", model:[key: "value"]) passData { // Do some processing here name = ...

C++ Meta Templates: A Good or Bad Design Choice?

I'm curious to find out if and when C++ meta templates are a good design choice for systems small to large. I understand that they increase your build time in order to speed up your execution time. However, I've heard that the meta template code is inherently hard to understand by many developers, which could be a problem for a large gro...

Scoping of Open classes in Ruby versus MOP in Groovy

What I'm trying to find out is whether there is some sort of equivalence to what I see in Groovy as ExpandoMetaClasses. I've been reading about Open Classes but I can't quite see what level of scoping Ruby allows of the class modifications. Borrowing an example from the blog above, in Groovy, I could modify Java's String class and add ...

Strange behavior: Hash's keys cancel dynamic method definition

Let's say I want some instance of String behave differently from other, "normal" instances - for example cancel the effect of the "upcase" method. I do the following: class String def foo def self.upcase self end self end end It seems to work fine, and the way I need it: puts "bar".upcase #=> "BAR" puts "bar".fo...

Graphically Modeling Metaprogramming

Are there any tools out there that let you model how a class (or a class hierarchy) can change at runtime? For example, if I have a given number of mixin classes that will be combined at runtime and I don't know which ones will be combined until the program runs, how do you go about diagramming that type of runtime behavior? Here's a be...

Get the value of an instance variable given its name

In general, how can I get a reference to an object whose name I have in a string? More specifically, I have a list of the parameter names (the member variables - built dynamically so I can't refer to them directly). Each parameter is an object that also has an from_s method. I want to do something like the following (which of course do...

How to use the [mixin] tag in AS3 applications?

I have the following two projects in in Flex Builder 3: One AS3 library project (generates a SWC file) One Flex application project (MXML Application) The MXML Application references to the AS3 library project (Flex build path). So far, so good. I now want to run code automatically when an application uses the AS3 library. The [mixin...

Can one unroll a loop when working with an integer template parameter?

I have the following code: template <int size> inline uint hashfn( const char* pStr ) { uint result = *pStr; switch ( size ) { case 10: result *= 4; result += *pStr; case 9: result *= 4; result += *pStr; ... ... case 2: result *= 4; result += *p...

MetaPython: Adding Methods to a Class

I would like to add some methods to a class definition at runtime. However, when running the following code, I get some surprising (to me) results. test.py class klass(object): pass for i in [1,2]: def f(self): print(i) setattr(klass, 'f' + str(i), f) I get the following when testing on the command line: >>> impor...

Ruby Template Module

Assume I have a family of related modules: module Has_Chocolate def has_chocolate? true end end module Has_Cake def has_cake? true end end . . . How would I construct a template module Has_*Something* where Something would be a parameter to the module? ...

Send allows access to private variables

Hello! Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set class_name, klass end After I do: create_class('User', ActiveRecord::Base) the following is ok: Object.send(:remove_const, :User) but this: Object.remove_const :User results in th...

Is MPS (a meta-programming language) worth looking into?

Another project from Jetbrains, their new issue tracker Charisma was written entirely in MPS. Is the concept useful/practical, or is it too soon? ...