metaprogramming

Overriding method calls in Ruby?

I'm trying to get a callback when any method on a particular class is called. Overriding "send" doesn't work. It seems send doesn't get called in normal Ruby method invocation. Take the following example. class Test def self.items @items ||= [] end end If we override send on Test, and then call Test.items, send doesn't get call...

Rails Workflow Gem - Metaprogramming events into named_scopes?

I'm using http://github.com/geekq/workflow to provide a state machine. I'm using ActiveRecord to save state, which means I have a "workflow_state" attribute in the model. I think I want a named_scope for each event in the state machine, so I can find all objects in a given state. For example, assuming a very simple state machine: workfl...

How do you overload dynamically extended methods in ruby?

I have some modules in an array. All the modules define a method called "process", and I'd like to call each of these process methods in sequence. The code I have looks something like this(assume the modules are all defined inside the Mod class): modules.each do |mod| extend Mod.const_get(mod) process(data) end This works fine for...

In the generic programming/TMP world what exactly is a model / a policy and a "concept" ?

I'd like to know the precise yet succinct definitions of these three concepts in one place. The quality of the answer should depend on the following two points. Show a simple code snippet to show how and what the concept/technique is used for. Be simple enough to understand so that a programmer without any exposure to this area can gra...

Is it possible to create and initialize an array of values using template metaprogramming?

I want to be able to create an array of calculated values (let's say for simplicity's sake that I want each value to be the square of it's index) at compile time using template metaprogramming. Is this possible? How does each location in the array get initialized? (Yes, there are easier ways to do this without resorting to template m...

How is Gant able to reference targets by their names, and not get a missing property exception?

In the code presented here: Gant file, there is the following code: target(dist: 'Create release artefacts') { depends(test) depends(jar) } target(run: 'Run the distributed jar') { depends(dist) Ant.java(jar: distributedJarPath, fork: 'true') } target(hello: 'Hello World') { println("Hello World"...

Intercept service method calls using metaprogramming in groovy/grails

Hi, I have a several Grails services that are invoked from Flex code using Spring BlazeDS integration. I wanted to add some general debug logging using the groovy metaClass. I have the following in a bootstrap class: class MyBootStrap { def grailsApplication def init = { servletContext -> initServiceCallLogging() } def destr...

How can I overide a ruby on rails module method via metaprogramming

As much I would like to I still don't know how to implement metaprogramming. For views rendered from my Admin::Base controller I would like override the Rails compute_public_path located under ActionView::Helpers::AssetTagHelper, so that all my admin's layout files will be under public/admin. There is probably a better way to this, but...

Ruby: context switches when calling dynamically defined methods

Here's some test code that explains the issue I'm having. The Child class calls methods on the Parent class. One of the Parent's methods defines a new method called foo on the Parent. After foo is defined, attempting to call it from Child class works, but the context is completely different (I can only access Child's instance variables, ...

Is there a way to find out whether a class is a direct base of another class?

Hi I'm wondering whether there is a way to find out whether a class is a direct base of another class i.e. in boost type trait terms a is_direct_base_of function. As far as I can see boost doesn't see to support this kind of functionality which leads me to think that its impossible with the current C++ standard. The reason I want it is...

Adding class attributes using a for loop in Python

I was trying to generate a class from a dictionary: class attr: for key in objects_type: setattr(attr, key, lambda cl: list()) This gives the error that attr is not defined during the for loop. I know I could write: class attr: pass for key in objects_type: setattr(attr, key, lambda cl: list()) But I am sure I r...

Automatic transformation from getter/setter to properties

I have a big libray written in C++ and someone created an interface to use it in python (2.6) in an automatic way. Now I have a lot of classes with getter and setter methods. Really: I hate them. I want to reimplement the classes with a more pythonic interface using properties. The problem is that every class has hundreds of getters and...

easing c++ to objective-c/cocoa bridging via metaprogramming?

In a pure C++ world we can generate interfacing or glue code between different components or interfaces at compile time, using a combination of template-based compile-time and runtime-techniques (to e.g. mostly automatically marshall to/from calls using legacy types). When having to interface C++ applications with Objective-C/Cocoa fo...

How can I convert this code to meta-programming, so I can stop duplicating it?

I've got a small but growing framework for building .net systems with ruby / rake , that I've been working on for a while now. In this code base, I have the following: require 'rake/tasklib' def assemblyinfo(name=:assemblyinfo, *args, Albacore::AlbacoreTask def execute(name) asm = AssemblyInfo.new asm.load_config_by_ta...

String to method/function call in ruby?

Hi I want to make my rails controller more flexible and try to create some Meta foo for it. I have a problem with the redirect_to method. Can I convert the "edit_admin_post_path()" method from a string or better read out the controller name and pass it dynamicly? this is my code for "post" in my Admin::Posts controller. respond_to do...

Using yield inside define_method in Ruby

Is it possible to make yield keyword work inside a block given to define_method? Simple example: class Test define_method :test do |&b| puts b # => #<Proc:...> yield end end Test.new.test { puts "Hi!" } This code produces following error in both Ruby 1.8.7 and 1.9.0: test.rb:4:in `test': no block given (LocalJump...

How do I get constants defined by Ruby's Module class via reflection?

I was trying to get Matz-n-Flanagan's RPL book's metaprogramming chapter into my head. However I couldn't understand the output from the following code snippet that I dreamed up. p Module.constants.length # => 88 $snapshot1 = Module.constants class A NAME=:abc $snapshot2 = Module.constants p $snapshot2.length ...

Is it possible to evaluate a Ruby DSL in a non-global context?

I'm using Blockenspiel to create a DSL with Ruby. It works great and solves a lot of my problems, but I encountered the following problem that is not strictly related to Blockenspiel. Suppose I have a DSL that looks like this: dish do name = 'Pizza' ingredients = ... nutrition_facts = ... end dish do name = 'Doner' ingredie...

Python equivalent for Ruby's ObjectSpace?

Hello guys, I've a name of a class stored in var, which I need to create an object from. However I do not know in which module it is defined (if I did, I would just call getattr(module,var), but I do know it's imported. Should I go over every module and test if the class is defined there ? How do I do it in python ? What if I have the ...

Groovy Prototype Object

I have a method with an incoming variable, which represents a script. e.g. hello.groovy Foo.init(this) Foo.groovy class Foo { static init(app) { } } What is the best way to add a ton of new functionality to the app variable in the init method? Basically, I would like to add all the functionality of another object to t...