metaprogramming

Ruby Plugin Architecture

I'd like a very basic example of a tiny base program, that reads in two plugins and registers them. These two plugins hook into the base program in the same way in a unconflicting manner. I'm very new to metaprogramming in any programming language for that matter, I'm not sure where to start. ...

C++: Are YOU using Loki or Boost for functors ?

Hi all, I've been reading Alexandrescu's book, Modern C++ design , and I've been quite impressed by the techniques he uses, so I wanted to add Loki library to my application. However, after further investigation, I saw that boost, that I'm already using, provides a lot of similar functionality (not all though, I couldn't find a singl...

What's the best way to have a variable number of template parameters ?

Please consider this -probably poorly written- example : class Command; class Command : public boost::enable_shared_from_this<Command> { public : void execute() { executeImpl(); // then do some stuff which is common to all commands ... } // Much more stuff ... private: virtual void e...

How to dynamically alter inheritance in Ruby

I would like to dynamically specify the parent class for a class in Ruby. Consider this code: class Agent def self.hook_up(calling_class, desired_parent_class) # Do some magic here end end class Parent def bar puts "bar" end end class Child def foo puts "foo" end Agent.hook_up(self, Parent) end Child.new.b...

ruby lambda context

Lets say I have the following ruby definition at the topmost level callable = lambda {"#{hi}"} and suppose that later on I create an object called temp that has a method called hi. Now what I would like to do is call callable within the context of temp. I have tried doing temp.instance_eval do callable.call end but this gives me th...

Programmatically derive a regular expression from a string.

I would like to input a string and return a regular expression that can be used to describe the string's structure. The regex will be used to find more strings of the same structure as the first. This is intentionally ambiguous because I will certainly miss a case that someone in the SO community will catch. Please post any and all po...

Python/Caml/Scala/YourSuggestion to PHP translator?

Hi! I'm looking for a translator of the source code written in some decent language to PHP. Ideally, the target PHP code should be human readable, cuz I'm not the only person working on the project. But the matter is, I would like to facilitate my personal developement process by bringing a high level language still allowing for easy m...

Need meta-programming magic to define a mother lode of bit fields in an error-free way.

The goal is to control which types of users are allowed to perform which operations at the UI level. This code has been in place for a while; I just want to improve it a bit. The file which I am trying to improve should probably be auto-generated, but that would be too big of a change, so I seek a simpler solution. A file which we shall...

Static check const char* contains spaces

Is there a way to check (assert) at compile time wether a const char* contains spaces or not? Something like: const char* cstr1 = "ok"; const char* cstr2 = "very bad"; check( cstr1 ); //OK check( cstr2 ); //Fail to compile The type is the same, but it may be possible to define some tricky template metaprogramming tecnique to do it. ...

Ruby singleton methods with (class_eval, define_method) vs (instance_eval, define_method)

Metaprogramming in ruby is great because I constantly use it to emulate prototype based programming and quickly write prototype solutions to some problems to test their viability. So I would like to know if there is any essential difference between the following pieces of code: (class << some_object; self; end).class_eval do define_me...

lua - capturing variable assignments

Ruby has this very interesting functionality in which when you create a class with 'Class.new' and assign it to a constant (uppercase), the language "magically" sets up the name of the class so it matches the constant. # This is ruby code MyRubyClass = Class.new(SuperClass) puts MyRubyClass.name # "MyRubyClass" It seems ruby "captures...

Using C++ Macros To Check For Variable Existence

I am creating a logging facility for my library, and have made some nice macros such as: #define DEBUG myDebuggingClass(__FILE__, __FUNCTION__, __LINE__) #define WARING myWarningClass(__FILE__, __FUNCTION__, __LINE__) where myDebuggingClass and myWarningClass both have an overloaded << operator, and do some helpful things with log me...

ruby - why TrueClass and FalseClass instead of Boolean?

I was working on serializing values when found out about this one. Ruby has a TrueClass class, and a FalseClass class, but it has no Boolean class. I'd like to know why is this. I see some advantages in using a Boolean - for example, String parsing could be centralized on it. Ruby developers are smarter than me, so there must be a lot ...

How do I run code before and after a method in a sub class?

My first thoughts are some thing like this: class AbstractBuilder attr_reader :time_taken def build_with_timer started_at = Time.now build @time_taken = Time.now - started_at end def build raise 'Implement this method in a subclass' end end class MyBuilder < AbstractBuilder def build sleep(5) end en...

Meta-programming: output method body as text

I'm dynamically defining a method in a module, and I'd like to check that once the method is bound to a class instance that the body of the method is what I'm expecting. Is there a way to output (as text) of the body of a method? Module controller_mixins.rb: module ControllerMixin instance_eval "def search_by_vendor (*args) \n" \ ...

gcc template error

why in the first line of this code: template <typename VectorType> std::string repr_vector_dynamic(const VectorType* vect) { std::stringstream strs; strs << "("; for (int i = 0; i < vect->size(); ++i) { if (0 != i) { strs << ", "; } strs << (*vect)[i]; } strs << ")"; return str...

Defining "method_called".. How do I make a hook method which gets called every time any function of a class gets called?

I want to make a hook method which gets called everytime any function of a class gets called. I have tried method_added, but it executes only once at the time of class definition, class Base def self.method_added(name) p "#{name.to_s.capitalize} Method's been called!!" end def a p "a called." end def b p "b called...

Are annotations some sort of DSL in Java?

After a bunch of XML config files, I've seen Java moving to Annotation based configurations. Are annotations playing the role of DSL here? Is it because the static nature of Java? I'm thinking in Ruby which doesn't have ( afaik ) things like that. Is it because Ruby has good metaprogramming capabilities? Are there alternatives ( I m...

boost static_assert with message?

Hi, on 1.43 boost it seems that BOOST_STATIC_ASSERT just allows to put a boolean value, is there some alternative that allows me to display a message as well on the compile error? ...

Convert array to PHP code

Following my last post about better syntax for arrays, I've decided to use JSON format for arrays and use a method to convert them into PHP code. Note that the ultimate goal of this is to be write a $config array in JSON and translate that into PHP code (so I can avoid having to use PHP's ugly array syntax): The function works fine fo...