metaprogramming

Accessing a class's containing namespace from within a module

I'm working on a module that, among other things, will add some generic 'finder' type functionality to the class you mix it into. The problem: for reasons of convenience and aesthetics, I want to include some functionality outside the class, in the same scope as the class itself. For example: class User include MyMagicMixin end # S...

python metaprogramming

I'm trying to archive a task which turns out to be a bit complicated since I'm not very good at Python metaprogramming. I want to have a module locations with function get_location(name), which returns a class defined in a folder locations/ in the file with the name passed to function. Name of a class is something like NameLocation. So...

Debugging metaprograms

Hi, Is there any way to check step by step what's going on in let's say template? I mean how it is instantiated step by step and so on? In book I've mentioned here , I found (2 minutes ago) quite interesting example of how binary could be implemented as a metafunction. template <unsigned long N> struct binary { static uns...

Cannot cout data

Hi, Guys, reffering to last post I'm trying to output data while template is instantiated template <unsigned long N> struct binary { std::cout << N;//<---------------------------------I'M TRYING HERE static unsigned const value = binary<N/10>::value << 1 // prepend higher bits | N%10; ...

Generic callbacks

Extends Related So, I'm trying to learn template metaprogramming better and I figure this is a good exercise for it. I'm trying to write code that can callback a function with any number of arguments I like passed to it. // First function to call int add( int x, int y ) ; // Second function to call double square( double x ) ; // T...

Ruby - How to remove a setter on an object

Given a class like this: class B class << self attr_accessor :var end end Suppose I can't modify the original source code of class B. How might I go about removing the setter on the class variable var? I've tried using something like B.send("unset_method", "var="), but that doesn't work (nor does remove_method, or ov...

Modify default queryset in django

I have added a 'cancelled' field to my model, is there a way to modify the model default query to something like cancelled=False ? without having to modify all my filter/exclude queries ? ...

Dynamically defined setter methods using define_method?

I use a lot of iterations to define convenience methods in my models, stuff like: PET_NAMES.each do |pn| define_method(pn) do ... ... end but I've never been able to dynamically define setter methods, ie: def pet_name=(name) ... end using define_method like so: define_method("pet_name=(name)") do ... end Any ideas? Thanks in adv...

Tutorials and Introductions to C++ Expression Templates

What are good introductions to the creation of C++ expression template systems? I would like to express arithmetic on user defined types while avoiding temporary values (which may be large), and to learn how to do this directly rather than applying an existing library. I have found Todd Veldhuizen's original paper and an example from th...

Define Instance Variable Outside of Method Defenition (ruby)

Hi all, I am developing (well, trying to at least) a Game framework for the Ruby Gosu library. I have made a basic event system wherebye each Blocks::Event has a list of handlers and when the event is fired the methods are called. At the moment the way to implement an event is as follows: class TestClass attr_accessor :on_close ...

How can I write a function template for all types with a particular type trait?

Consider the following example: struct Scanner { template <typename T> T get(); }; template <> string Scanner::get() { return string("string"); } template <> int Scanner::get() { return 10; } int main() { Scanner scanner; string s = scanner.get<string>(); int i = scanner.get<int>(); } The Scanner class i...

Why is partial specialziation of a nested class template allowed, while complete isn't?

template<int x> struct A { template<int y> struct B {};. template<int y, int unused> struct C {}; ...

Rails Metaprogramming: How to add instance methods at runtime?

I'm defining my own AR class in Rails that will include dynamically created instance methods for user fields 0-9. The user fields are not stored in the db directly, they'll be serialized together since they'll be used infrequently. Is the following the best way to do this? Alternatives? Where should the start up code for adding the meth...

Metaprogramming - self explanatory code - tutorials, articles, books

Hello everybody, I am looking into improving my programming skils (actually I try to do my best to suck less each year, as our Jeff Atwood put it), so I was thinking into reading stuff about metaprogramming and self explanatory code. I am looking for something like an idiot's guide to this (free books for download, online resources). A...

Ruby on Rails associations for set theory (union, difference, intersection, etc.)

I would like a has_many association that works like so: class Hood acts_as_tree has_many :houses, :union_with => :parent end class House end where any House associated with Hood 1 would also be returned in .houses of subhoods of Hood 1, along with the subhoods' individual associations. The association only needs to work from the...

C++ template meta-programming, number of member variables?

hello Is it possible in C++ to determine number of variables/fields in the generic class? for example // suppose I need metaclass number_members determines number of members struct example { int i, j; }; assert(number_members<example>::value==2); I looked through mpl but could not find implementation. thanks. ...

About variadic templates

Hi, I'm currently experiencing with the new c++0x variadic templates, and it's quite fun, Although I have a question about the process of member instantiation. in this example, I'm trying to emulate the strongly typed enum with the possibility of choose a random valid strong enum (this is used for unit testing). #include<vector> #in...

Ruby - print the variable name and then its value

What is the best way to write a function (or something DSLish) that will allow me to write this code in Ruby. How would I construct the function write_pair? username = "tyndall" write_pair username # where write_pair username outputs username: tyndall Is it possible to do? Looking for the most simple way to do this. ...

How do you pass self to class_eval in ruby?

...

Testing a rake task in rspec (and cucumber)

I'm new to Ruby, and I've been trying to learn Rake, RSpec, and Cucumber. I found some code that will help me test my Rake tasks, but I'm having trouble getting it to work. I was told here: http://blog.codahale.com/2007/12/20/rake-vs-rspec-fight/to drop this: def describe_rake_task(task_name, filename, &block) require "rake" des...