metaprogramming

How to evaluate methods of another class in current context?

I have 2 classes whose object should act as "partners". The first one is my Thing class, whose instances should act as Tree::TreeNodes of the gem RubyTree. Basically, this delegation can be implemented using Forwardable: class Thing < NoClassInheritancePlease extend Forwardable def initialize(title = "node") @node = Tree::Tree...

Using metamorphic code to reduce boilerplate

Has anyone seen metamorphic code -- that is, code that generates and runs instructions (including IL and Java Bytecode, as well as native code) -- used to reduce boilerplate code? Regardless of the application or language, typically one has some database code to get rows from the database and return a list of objects. Of course, there ...

Where to create classes dynamically? Within Object or global namespace?

I am creating non-namespaced classes dynamically. I have seen an example where this creation is done within the Object class: class Object for elem in ARRAY sub_class = Object.const_set(elem.to_s.camelize, Class.new(SuperClass)) sub_class.class_eval do def initialize(*args, &block) ... super *args, &b...

PostSharp 1.5 and .Net 4

Postsharp is great, but only the 1.5 version is still opensource. Does it work with .net 4.0? If not, are there any other good AOP weavers out there? I'm not interested in the proxy type. ...

Reading/Writing self heap

Could the own heap space be readed? could the software be self modified in memory? I write some code to show the subject, am I reading own code at memory? how (if possible) to write it and change instruction on runtime? #include<stdio.h> #include<stdint.h> volatile int addressBase; uint8_t read(int address); int main(void) { ...

ScopeGates in Ruby

I am reading Metaprogramming in Ruby book. In that book, when I was reading about scopegates, the following code was shown my_var = "Success" MyClass = Class.new do puts "#{my_var} in the class definition" define_method :my_method do puts "#{my_var} in the method" end end MyClass.new.my_method =>Success in the class defini...

Using built-in type(,,) function to create a dynamic module

I'm trying to use the type(,,) function to dynamically build a module. The module creates classes representing templates, and I need a new class for every .tex file that lives in a particular folder. For instance, if I have a a4-page-template.tex file, I need to create a class called A4PageTemplate. I can create the type easily enough u...

What's the use of metaprogramming?

I've read: Wikipedia Code Generation vs. Metaprogramming The art of Metaprogramming Metaprogramming at c2.com and I confess some confusion at the purpose behind metaprogramming/code generation. Does anyone have a concrete example of where they use metaprogramming/code generation? Even better would be an accompanying explanation of w...

How do I see where in the Class Hierarchy a Method was defined and Overridden in Ruby?

Is there a way to know whether or not a method has been overridden by a subclass programmatically? Something that works like this: class BaseModel def create puts "superclass" end end class SomeModel < BaseModel def create puts "subclass" end end puts SomeModel.overridden_instance_methods #=> [:create] Any ideas? ...

How do you list included Modules in a Ruby Class?

How would you list out the modules that have been included in a specific class in a class hierarchy in Ruby? Something like this: module SomeModule end class ParentModel < Object include SomeModule end class ChildModel < ParentModel end p ChildModel.included_modules #=> [SomeModule] p ChildModel.included_modules(false) #=> [] Li...

Compile-time 'String' Manipulation with Variadic Templates

Hey all, I'm currently trying to write a compile-time string encryption (using the words 'string' and 'encryption' quite loosely) lib. What I have so far is as follows: // Cacluate narrow string length at compile-time template <char... ArgsT> struct CountArgs { template <char... ArgsInnerT> struct Counter; template <char Cur, char.....

Creating MySQL stored procedures using MySQL

Is there a way to programmatically create stored procedures using MySQL? I can write a stored procedure to create databases and tables using prepared statements, but I get an error message saying it is not supported to create stored procedures with prepared statements. I realize I can do this in PHP or Java, but so far I have been kee...

Implementing lazy-loaded modules in VBScript

A while back, I needed a solution to sanely import libraries in VBScript. VBScript, for reference, has no build-in import capabilities. The traditional method of importing files is to use SSI, which dumps the contents of the includee verbatim into the includer. This is less-than-optimal for a number of reasons: there is no way to avoid ...

What happens when you run class_eval on something that's not a class?

test = "a" test.class_eval do # what is going on here??? end ...

Controlling the instantiation of python object

My question does not really have much to do with sqlalchemy but rather with pure python. I'd like to control the instantiation of sqlalchemy Model instances. This is a snippet from my code: class Tag(db.Model): __tablename__ = 'tags' query_class = TagQuery id = db.Column(db.Integer, primary_key=True) name = db.Column(d...

Access result type of a function template parameter in the template?

Given the following template: template<class T> class Container { private: boost::function<T> f; }; ... and its instantiation, perhaps as follows: Container<bool(int, int)> myContainer; , is there a way to access the return type of the function description and compile conditionally against it? For example, if the caller...

What is best way to execute a Ruby program from a Ruby program?

I would like to do something like this from a Ruby script, within a loop: Write a file a.rb (which changes each iteration) execute system(ruby 'a.rb') a.rb writes a string with results to a file 'results' a.rb finishes and Ruby returns 'true' (assuming no errors) the calling script reads the file 'results' and takes action. I expect ...

Are Dynamic Class Methods possible in Ruby?

Possible Duplicate: How do I use define_method to create class methods? I am trying to do this: class Foo class << self def runtime_method(*methods) methods.each do |name| define_method "self.#{name}" do |*args| "dynamic class method #{name.inspect}" end self.class_eval do ...

When are modules included in a Ruby class running in rails?

I'm trying to write a method that tells me every class that includes a particular Module. It looks like this - def Rating.rateable_objects rateable_objects = [] ObjectSpace.each_object(Class) do |c| next unless c.include? Rateable rateable_objects << c end rateable_objects end Where "Rateable" is my module that I'm in...

Arbitrary number of nested-loops?

I'm looking to take an arbitrary number of lists (e.g. [2, 1, 4 . . .], [8, 3, ...], . . .) and pick numbers from each list in order to generate all permutations. E.g.: [2, 8, ...], [2, 3, ...], [1, 8, ...], [1, 3, ...], [4, 8, ...], [4, 3, ...], ... This is easily accomplished using nested for-loops, but since I'd like to it accept...