metaprogramming

Ruby parameterize if ... then blocks

I am parsing a text file and want to be able to extend the sets of tokens that can be recognized easily. Currently I have the following: if line =~ /!DOCTYPE/ puts "token doctype " + line[0,20] @ast[:doctype] << line elsif line =~ /<html/ puts "token main HTML start " + line[0,20] html_scanner_off = false ...

C++ Declarative Parsing Serialization

Looking at Java and C# they manage to do some wicked processing based on special languaged based anotation (forgive me if that is the incorrect name). In C++ we have two problems with this: 1) There is no way to annotate a class with type information that is accessable at runtime. 2) Parsing the source to generate stuff is way to compl...

How do you evaluate a string as a clojure expression?

How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3 ...

variable name introspection in Python

Is it possible to dynamically determine the name of a variable in Python? For example, I sometimes have the following situation: name = foo if bar else baz type = alpha or bravo D = { "name": name, "type": type } It would be nice if duplication there could be reduced with something like D = makedict(name, type). Somewhat re...

loading/unloading/updating class in ruby

I did a little experiments with Ruby class dynamic loading/unloading/updating as implementing plugins infrastructure. I found a few points: If loading new version of same class without first unloading it, the new one essentially 'top' or 'merge' with previous version. All existent objects created with previous version would get their c...

ruby on rails add functionality to model property change

In my rails model, I have a decimal property called employer_wcb. I would like it if, when employer_wcb was changed, a dirty bit was set to true. I'd like to override the employer_wcb setter method. Any way to do so (in particular using metaprogramming)? ...

creating dynamic helper methods in rails

I am trying to create a bunch of dynamic helper methods like these: show_admin_sidebar show_posts_sidebar show_users_sidebar So far I have this in my helper.rb file: #spits out a partial def show_sidebar(name, show_sidebar = true) @content_for_sidebar = render :partial => "partials/#{name}" @show_sidebar =...

dynamically create a class without a namespace

I am trying to dynamically create a class using the eval method. It is working fine except for one small problem. As my code shows I am creating the Browser class inside the BrowserFactory class. When I do this the Browser class has an added namespace of BrowserFactory. Is there anyway to evaluate the Browser class from a string with...

__getattr__ keeps returning None even when I attempt to return values

Try running the following code: class Test(object): def func_accepting_args(self,prop,*args): msg = "%s getter/setter got called with args %s" % (prop,args) print msg #this is prented return msg #Why is None returned? def __getattr__(self,name): if name.startswith("get_") or name.startswith("set_"): prop = name[...

Using define_method to define global methods outside of a module

I'd like to write this: [:p, :h1, :h3].each do |tag| define_method(tag) { |text| "<#{tag}>#{text}</#{tag}>" } end It's just some simple methods to wrap text in HTML tags. I want to be able to use these methods in the rest of the script. Unfortunately the define_method method seems to only work inside of a module. But if I did thi...

SFINAE to check for inherited member functions

Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo(), but not that B inherits one): #include <iostream> template<typename T, typename Sig> ...

How can I determine the "caller" of my method in Objective-C?

So I have a rather complex application that I've, perhaps naively, agreed to "debug". Upon entering into a certain method, I'd like to print out as much info about who called the method, from which class, method, etc. it was called from. Any suggestions would be very much appreciated!! ...

Generating helper function module

I a writing a DSL to generate parsers for bioinformatics flat files. I would like to let the user define helper functions in block and then include the function in the parsing context object. I would like to use a syntax like: rules = Rules.new do helpers do def foo() #... end def bar( baz ) #... end ...

GCC error with variadic templates: "Sorry, unimplemented: cannot expand 'Identifier...' into a fixed-length arugment list"

While doing variadic template programming in C++0x on GCC, once in a while I get an error that says "Sorry, unimplemented: cannot expand 'Identifier...' into a fixed-length arugment list." If I remove the "..." in the code then I get a different error: "error: parameter packs not expanded with '...'". So if I have the "..." in, GCC c...

Calling super on a method defined by define_method

I have created a Model class where I define methods based on a method (attribute) called in User (which inherits from Model). The problem is that I cannot override the method defined by define_method, and call super to pass to the defined method. I guess this is because the defined method is added to User itself, and not to the Model, so...

Extract the return type of a function without calling it (using templates?)

I'm looking for a way in C++ to extract the return type of a function (without calling it). I presume this will require some template magic. float Foo(); int Bar(); magic_template<Foo>::type var1; // Here 'var1' should be of type 'float' magic_template<Bar>::type var2; // and 'var2' should be of type 'int' I am currently investigati...

c++ create continguous array of type which is not known at compiletime

Hi! let's say i have a few different structure definitions (in fact, i have arround 50 such definitions): struct Type1{ int i; float f; }; struct Type2{ bool b1; bool b2; double d; }; they are all POD, but can contain completely different data. now, at runtime, i want to decide what type of those i need and then ...

Does calling the constructor of an empty class actually use any memory?

Suppose I have a class like class Empty{ Empty(int a){ cout << a; } } And then I invoke it using int main(){ Empty(2); return 0; } Will this cause any memory to be allocated on the stack for the creation of an "Empty" object? Obviously, the arguments need to be pushed onto the stack, but I don't want to incur any extra ...

What use does ./configure serve (other than checking dependencies)

Why does every source package that uses a makefile come with a ./configure script, what does it do? As far as I can tell, it actually generates the makefile? Is there anything that can't be done in the makefile? ...

How are variables bound to the body of a define_method?

While trying to brush up my Ruby skills I keep running across this case which I can't figure out an explanation for by just reading the API docs. An explanation would be greatly appreciated. Here's the example code: for name in [ :new, :create, :destroy ] define_method("test_#{name}") do puts name end end What I want/expect to...