metaprogramming

Ruby - convert from symbol to variable

How can I convert :obj back into a variable called obj inside the def? def foo(bar) bar.some_method_call end foo :obj UPDATE: The final code is more elaborate than this but... I like to be able to say foo :obj instead of foo obj I working on some DSL-like syntax. And this one change would let things read a little clearer. ...

Conditionally change C statements in C source file

I have a C file in which we are moving the logging infrastructure. So if ( logging_level >= LEVEL_FINE ) printf("Value at %p is %d\n", p, i); becomes do_log2(LEVEL_FINE, "Value at %p is %d\n", _ptr(p), _num(i)); do_log2 means log with 2 arguments. So I need a C parsing and modification infrastructure to do this. Which tool can...

List stored functions using a table in PostgreSQL

Just a quick and simple question: in PostgreSQL, how do you list the names of all stored functions/stored procedures using a table using just a SELECT statement, if possible? If a simple SELECT is insufficient, I can make do with a stored function. My question, I think, is somewhat similar to this other question, but this other question...

Custom C++ Preprocessor / Typeful Macros

Having seen the advantages of metaprogramming in Ruby and Python, but being bound to lower-level languages like C++ and C for actual work, I'm thinking of manners by which to combine the two. One instance comes in the simple problem for sorting lists of arbitrary structures/classes. For instance: struct s{ int a; int b; }; vector<s...

C++ templates problem

I have defined a generic tree-node class like this: template<class DataType> class GenericNode { public: GenericNode() {} GenericNode(const DataType & inData) : mData(inData){} const DataType & data() const { return mData; } void setData(const DataType & inData) { mData = inData; } size_t getChildCount() ...

How to get the nested modules dynamically from an object?

given: module A class B end end b = A::B.new we want to be able to get the module nesting as an array. This can be done if the class is known in advance. eg: module A class B def get_nesting Module.nesting # => [A::B, A] end end end But, how to do it for an arbitrary object, so that we could do something lik...

Metaprogramming a wizard and database rules

Our software has a few places where it uses wizards to collect information from users then generates a bunch of database entries based on what users enter into those wizards. Currently these wizards and the rules for generating database entries from the wizards are handled by many kLOC of unmaintainable C++ code. In the spirit of The P...

ruby metaprogramming - yield block not working in dynamically added method

I'm working on extending the NotAMock framework for stubbing methods in rspec, and getting the stubs to yield to a methods block. The code in this Gist works perfectly when I code it on my own (which is done-up to resemble how NotAMock stubs methods). but when I incorporate the object.instance_eval... code into the NotAMock framework, ...

Can Ruby operators be aliased?

I'm interested in how one would go in getting this to work : me = "this is a string" class << me alias :old<< :<< def <<(text) old<<(text) puts "appended #{text}" end end I'd like that when something gets appended to the me variable, the object will use the redefined method. If I try to run this, I get syntax error, une...

How do I undo meta class changes after executing GroovyShell?

For example, if I execute a Groovy script, which modifies the String meta class, adding a method foo() GroovyShell shell1 = new GroovyShell(); shell1.evaluate("String.metaClass.foo = {-> delegate.toUpperCase()}"); when I create a new shell after that and execute it, the changes are still there GroovyShell shell2 = new GroovyShell(); ...

Groovy: adding methods to instances and classes with metaClass doesn't work?

See the code below. Old instances of a class created before a method is added to the class using metaClass should not understand the method right? The assert statement below the 'PROBLEMATIC LINE' comment is executed when I think it should not be, as the old parentDir instance should not understand the blech() message. // derived from...

dynamically adding functions to a Python module

Our framework requires wrapping certain functions in some ugly boilerplate code: def prefix_myname_suffix(obj): def actual(): print 'hello world' obj.register(actual) return obj I figured this might be simplified with a decorator: @register def myname(): print 'hello world' However, that turned out to be rat...

How can I find the names of argument variables passed to a block

Im trying to do some metaprogramming and would like to know the names of the variables passed as block arguments: z = 1 # this variable is still local to the block Proc.new { |x, y| local_variables }.call # => ['_', 'z', x', 'y'] I am not quite sure how to differentiate between the variables defined outside the block and the bloc...

How to add a new closure to a class in groovy.

From Snipplr Ok here is the script code, in the comments is the question and the exception thrown class Class1 { def closure = { println this.class.name println delegate.class.name def nestedClos = { println owner.class.name } nestedClos() } } def clos = new Class1().closure ...

A guide to Boo's metaprogramming and extensibility features?

I'm interested in learning about Boo's more powerful features such as syntactic macros, parser support (Ometa?), compiler pipeline, etc. My impression is that these areas have been in flux and somewhat under-documented. Are there any good resources for learning about these things other than studying the source code? ...

Template specialization problem

Hi, I'm trying really hard to made this work, but I'm having no luck. I'm sure there is a work around, but I haven't run across it yet. Alright, let's see if I can describe the problem and the needs simply enough: I have a RGB template class that can take typenames as one of its template parameters. It takes the typename and sends it...

How do I invoke a non-default constructor for each inherited type from a type list?

I'm using a boost typelist to implement the policy pattern in the following manner. using namespace boost::mpl; template <typename PolicyTypeList = boost::mpl::vector<> > class Host : public inherit_linearly<PolicyTypeList, inherit<_1, _2> >::type { public: Host() : m_expensiveType(/* ... */) { } private: const ExpensiveType m...

Detecting that a method was not overridden

Say, I have the following 2 classes: class A def a_method end end class B < A end Is it possible to detect from within (an instance of) class B that method a_method is only defined in the superclass, thus not being overridden in B? Update: the solution While I have marked the answer of Chuck as "accepted", later Paolo Perrota m...

__FILE__ macro manipulation handling at compile time.

One of the issues I have had in porting some stuff from Solaris to Linux is that the Solaris compiler expands the macro __FILE__ during preprocessing to the file name (e.g. MyFile.cpp) whereas gcc on Linux expandeds out to the full path (e.g. /home/user/MyFile.cpp). This can be reasonably easily resolved using basename() but....if you're...

Using groovy metaClass to mock out Shiro SecurityUtils in bootstrap

For further background, see http://grails.markmail.org/message/62w2xpbgneapmhpd I'm trying to mock out the Shiro SecurityUtils.getSubject() method in my BootStrap.groovy. I decided on this approach because the Subject builder in the latest Shiro version isn't available in the current version of the Nimble plugin (which I'm using). I d...