Questions tagged [metaprogramming]

Metaprogramming is the capability to reprogram ones programming environment, as with macros or metaclasses.

Metaprogramming - the capability to reprogram ones programming environment - can save time and code by allowing one to create new abstractions, or new and more concise syntax for repetitive tasks.

Notable metaprogramming facilities include:

  • Common lisp macros
  • Scheme hygienic macros
  • Common lisp metaobject protocol
  • Python metaclass facility
  • Smalltalk's entirely transparent and reflective execution environment

For more info see http://en.wikipedia.org/wiki/Metaprogramming.

4722 questions
116
votes
6 answers

How do I use define_method to create class methods?

This is useful if you are trying to create class methods metaprogramatically: def self.create_methods(method_name) # To create instance methods: define_method method_name do ... end # To create class methods that refer to the…
Chinasaur
  • 2,108
  • 2
  • 16
  • 17
115
votes
12 answers

Compile time string hashing

I have read in few different places that using C++11's new string literals it might be possible to compute a string's hash at compile time. However, no one seems to be ready to come out and say that it will be possible or how it would be done. Is…
deft_code
  • 57,255
  • 29
  • 141
  • 224
114
votes
5 answers

How to call methods dynamically based on their name?

How can I call a method dynamically when its name is contained in a string variable? For example: class MyClass def foo; end def bar; end end obj = MyClass.new str = get_data_from_user # e.g. `gets`, `params`, DB access, etc. str #=> "foo" #…
user502052
  • 14,803
  • 30
  • 109
  • 188
113
votes
2 answers

Get the value of an instance variable given its name

In general, how can I get a reference to an object whose name I have in a string? More specifically, I have a list of the parameter names (the member variables - built dynamically so I can't refer to them directly). Each parameter is an object that…
LK__
  • 6,515
  • 5
  • 34
  • 53
106
votes
4 answers

How do I convert a Ruby class name to a underscore-delimited symbol?

How can I programmatically turn a class name, FooBar, into a symbol, :foo_bar? e.g. something like this, but that handles camel case properly? FooBar.to_s.downcase.to_sym
Josh Glover
  • 25,142
  • 27
  • 92
  • 129
104
votes
3 answers

What is the __dict__.__dict__ attribute of a Python class?

>>> class A(object): pass ... >>> A.__dict__ >>> A.__dict__.__dict__ Traceback (most recent call last): File "", line 1, in AttributeError: 'dictproxy' object has no attribute '__dict__' >>>…
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83
103
votes
4 answers

Ruby Metaprogramming: dynamic instance variable names

Let's say I have the following hash: { :foo => 'bar', :baz => 'qux' } How could I dynamically set the keys and values to become instance variables in an object... class Example def initialize( hash ) ... magic happens here... end end ...…
Andrew
  • 42,517
  • 51
  • 181
  • 281
90
votes
31 answers

Python vs. Ruby for metaprogramming

I'm currently primarily a D programmer and am looking to add another language to my toolbox, preferably one that supports the metaprogramming hacks that just can't be done in a statically compiled language like D. I've read up on Lisp a little and I…
dsimcha
  • 67,514
  • 53
  • 213
  • 334
86
votes
6 answers

What do "reify" and "reification" mean in the context of (functional?) programming?

I read this term a lot in blogs about haskell and functional programming (specially in sigfpe's blog) but I don't have a clue about what it means. I get away with not knowing it most of the times, but I probably would have understood the texts a lot…
81
votes
3 answers

Find classes available in a Module

I have a module MyModule. I dynamically load classes into it. How can I get a list of the classes defined within its namespace? Example: def load_plugins Dir.glob(File.dirname(__FILE__) + '/plugins/*.rb') do |f| MyModule.class_eval…
joshuaxls
  • 950
  • 1
  • 7
  • 11
81
votes
4 answers

Dynamic Class Definition WITH a Class Name

How do I dynamically define a class in Ruby WITH a name? I know how to create a class dynamically without a name using something like: dynamic_class = Class.new do def method1 end end But you can't specify a class name. I want to create a class…
Mike Bethany
80
votes
6 answers

How can I dynamically create class methods for a class in python

If I define a little python program as class a(): def _func(self): return "asdf" # Not sure what to resplace __init__ with so that a.func will return asdf def __init__(self, *args, **kwargs): setattr(self, 'func',…
user1876508
  • 12,864
  • 21
  • 68
  • 105
78
votes
11 answers

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…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
76
votes
11 answers

Does C++ support compile-time counters?

For the purpose of introspection, sometimes I've wanted to automatically assign serial numbers to types, or something similar. Unfortunately, template metaprogramming is essentially a functional language, and as such lacks global variables or…
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
76
votes
9 answers

List stored functions that reference 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…
Paolo B.
  • 969
  • 1
  • 7
  • 6