metaprogramming

Javascript automatic getter/setters (John Resig Book)

Hi, I'm reading "Pro Javascript Techniques" from John Resig, and I'm confused with an example. This is the code: // Create a new user object that accepts an object of properties function User( properties ) { // Iterate through the properties of the object, and make sure // that it's properly scoped (as discussed previously) for ( ...

Refactor with some dynamic programming?

I have a piece of code here that i really could use some help with refactoring. I need the different methods for adding relational data in a form in rails. The code is taken from http://railscasts.com/episodes/75-complex-forms-part-3, my problem is that i need to have the methods fro both the Material model and the Answer model. So i nee...

How can I dynamically add a method to the Math class in Ruby on Rails?

I am trying to add the following method to the Math class in Ruby on Rails: class Math def self.round_with_precision(number, precision) scalar = 10.0 ** precision number = number * scalar number = number.round number = number / scalar return number; end end I then added the following to my environment.rb: requ...

Can you nest C preprocessor directives?

For instance, is the following possible: #define definer(x) #define #x? Thanks. ...

Lazy Load External Javascript Files

I am trying to write a javascript class that loads script files as they are needed. I have most of this working. It is possible to use the library with the following Syntax: var scriptResource = new ScriptResource('location/of/my/script.js'); scriptResource.call('methodName', arg1, arg2); I would like to add some additional syntactic ...

Is there a good way of getting MethodInfo of open generic method?

Consider type like this one public interface IHaveGenericMethod { T1 Method<T1>(T1 parm); T2 Method<T1,T2>(T1 parm); int Method2(int parm); } How do I get a methodInfo for its methods? for a regular non-generic method, like method2, I can go with typeof(IHaveGenericMethod).GetMethod("methodName",new Type[]{typeof(itsParamete...

What's the best strategy for get/setting metadata on Ruby methods at runtime?

I have a class with some methods. It's supersekret but I've reproduced what I can here. Class RayGun # flashes red light # requires confirmation # makes "zowowowowowow" sound def stun! # ... end # flashes blue light # does not require confirmation # makes "trrrtrtrtrrtrtrtrtrtrtr" sound def freeze! # ... end...

Is there a library that generates UIs based on metadata declarations like this>>?

Hello, Do you know about a library that allows us to generate UI by just stating that it should be generated? I think there must be a person who have implemented a mechanism allowing us to transform code like this: class Main { @Command int add(int a, int b) { return a+b; } } into, say, a dialog with 2 text fields...

Best tutorial for embedding a scripting language

What is the best tutorial, recipe, how-to, to embed a scripting language (like python, lua...) in another language (like c#)? Particularly, one that shows how to provide host-level elements in the script-level language (e.g. whenever someone accesses the attribute name of the object 'car' in python, the host application intercepts that c...

The future of Naked Objects pattern (and UI auto-generation)

I ask about the pattern, not framework. This is kind of follow-up to a question on UI auto-generation. Do you believe in the concept of UI auto-generation from metadata? What kind of problems can be approached this way? The question arose when I've created a small library to support my student projects, which generates interactive CL...

C++ metaprogramming - generating errors in code

Is there a way that I can create a function that takes an int template parameter, and have that function give a compile time error if the value passed to the function is less than 10? The following code does not work, but it shows what I want to accomplish: template <int number1> void reportErrorIfLessThan10() { #if(number1 < 10) ...

what year came before 1 CE (aka AD 1)?

Was the year before that 0 CE or 1 BCE? This is a meta-programming question, relating to the religious wars over the first number in a list. Should it be 0 or 1? Some points to consider: What would Richard Stallman say? I read an article by him recently where he got upset that the OLPC is offering Windows. He listed the reasons th...

Can I autodiscover parameters to shell or Perl scripts to "meta" program WEB UIs for them?

Is it possible to auto-discover parameters to shell/Perl scripts in order to "meta" program WEB UIs for them? I have a bunch of "legacy" scripts that I'd like to "web wrap". So far I have created a CGI-BIN web app with about 3 parameters that can call a bash/Perl reporting script. But it now occurs to me maybe there is quicker or aut...

What did you do to learn language / framework XX?

As developers, we have to constantly learn about new languages and technologies. What did you do to learn the programming languages (or frameworks) you know now? Please list Programming language or framework Resource Either web page or Book Did you have any special projects for that language / Can you recommend a project to learn tha...

Best way to create a "runner" script in Python?

I have a bunch of Python modules in a directory, all being a derivate class. I need a "runner" script that, for each module, instantiate the class that is inside it (the actual class name can be built by the module file name) and than call the "go" method on each of them. I don't know how many modules are there, but I can list all of th...

Is anyone using meta-meta-classes / meta-meta-meta-classes in Python/ other languages?

I recently discovered metaclasses in python. Basically a metaclass in python is a class that creates a class. There are many useful reasons why you would want to do this - any kind of class initialisation for example. Registering classes on factories, complex validation of attributes, altering how inheritance works, etc. All of this be...

What exactly is metaprogramming?

I was reading an article on TheServerSide on ployglot programming on the Java platform. Some comments in the article refer to metaprogramming as the ability to generate code (perhaps on the fly). Is metaprogramming the ability to generate code on the fly or is it the ability to inject methods and attributes into existing objects at run...

Dynamic/runtime method creation (code generation) in Python

Hello, I need to generate code for a method at runtime. It's important to be able to run arbitrary code and have a docstring. I came up with a solution combining exec and setattr, here's a dummy example: class Viking(object): def __init__(self): code = ''' def dynamo(self, arg): """ dynamo's a d...

Groovy: Delegating metaclass for an Interface?

Using Groovy's package name convention, I can intercept Groovy method calls to a Java method like so: package groovy.runtime.metaclass.org.myGang.myPackage class FooMetaClass extends groovy.lang.DelegatingMetaClass { StringMetaClass(MetaClass delegate) { super(delegate); } public Object getProperty(Object a, St...

How can I cause a setter method to encrypt data before setting it in ActiveRecord?

I'd like to implement a Rails User model that has a DB column called password. I want to make it so that when I call... user_instance.password = 'cleartext' the method hashes the cleartext before setting it on the instance like so: Digest::SHA1.hexdigest(cleartext) I've tried using a callback, but the problem is that is hashes the ...