metaprogramming

list Rails controller instance variables

守給你的承諾、 提交于 2020-01-12 14:16:48
问题 i was trying to list the instance variables inside a controller but came up with irb>HomeController.instance_variable_names => ["@visible_actions", "@inheritable_attributes", "@controller_path", "@action_methods", "@_process_action_callbacks"] and I tried it on the action irb>HomeController.action("index").instance_variable_names => [] so what do controller Instance variables belong to? 回答1: The instance variables belong to the instantiated controller object, and are only created when the

Programmatically generate methods for a class

落花浮王杯 提交于 2020-01-12 14:13:10
问题 I have about 20 methods to redirect to a wrapper method that takes the original method, and the rest of the arguments: class my_socket(parent): def _in(self, method, *args, **kwargs): # do funky stuff def recv(self, *args, **kwargs): return self._in(super().recv, *args, **kwargs) def recv_into(self, *args, **kwargs): return self._in(super().recv_into, *args, **kwargs) # and so on... How can I add more of these methods programmatically? This is about as far as I get before everything starts to

Is it possible to figure out the parameter type and return type of a polymorphic C++ 14 lambda?

我是研究僧i 提交于 2020-01-12 08:08:53
问题 Starting from this question (Is it possible to figure out the parameter type and return type of a lambda?) I used the proposed function_traits a lot. However, with C++14 polymorphic lambdas have arrived and they gave me a hard time. template <typename T> struct function_traits : public function_traits<decltype(&T::operator())> {}; // For generic types, directly use the result of the signature of its 'operator()' template <typename ClassType, typename ReturnType, typename... Args> struct

Variable function name Javascript

谁都会走 提交于 2020-01-11 14:05:54
问题 I'm sorting array: myArray.sort(comparators.some_comparator); and I have several comparator to choose from: comparators = { asc_firstname_comparator : function(o1, o2){ ... } desc_firstname_comparator : function(o1, o2){ ... } etc... } I want to write function which returns certain comparator depending on input data. It should figure out comparator from string inputs, something like this: function chooseComparator(field, order){ return "comparators."+order+"_"+field+"_comparator"; } So is it

How to avoid simple recursive template typedefs

China☆狼群 提交于 2020-01-11 07:47:09
问题 I have the following simple problem: A class template<typename D> Parser which defines a ModuleType as Module<Parser> . I would like to inject the parser type into the module, as to be able to extract again several types from the parser in it. This is handy as one needs only one template parameter in Module. But the problem comes if the parser needs some types which are defined in the module such as OptionsType , accessing this in the Parser by the using declaration using ModuleOptions = ...

Groovy dynamic property per object

这一生的挚爱 提交于 2020-01-11 05:27:36
问题 Using Groovy 1.8. I'm trying to create a dynamic class definition that will cache properties per object. I did use propertyMissing without adding the property to the object just fine. I just think caching the properties would be more efficient. Right? Note that each instance must have its own different properties. The code below works fine: class C {} def c = new C() c.metaClass.prop = "a C property" println c.prop def x = new C() x.prop will output: a C property groovy.lang

Memory leak in django when keeping a reference of all instances of forms

爷,独闯天下 提交于 2020-01-07 05:44:05
问题 This is a followup to this thread. I have implemented the method for keeping a reference to all my forms in a array like mentioned by the selected answer in this but unfortunately I am getting a memory leak on each request to the django host. The code in question is as follows: This is my custom form I am extending which has a function to keep reference of neighboring forms, and whenever I instantiate a new form, it just keeps getting added to the _instances stack. class StepForm(ModelForm):

Scala: Get type name without runtime reflection and without type instance

老子叫甜甜 提交于 2020-01-07 02:44:08
问题 I'd like to get the name of a type, as a String, without runtime reflection. Using macros, and with an instance of the type, I can do it like this: def typeNameFromInstance[A](instance: A): String = macro typeNameFromInstanceImplementation[A] def typeNameFromInstanceImplementation[A]( c: Context)( instance: c.Expr[A]): c.Expr[String] = { import c.universe._ val name = instance.actualType.toString c.Expr[String](Literal(Constant(name))) } How can I do this without an instance of the type? I'd

Heterogeneous sequence generator

半腔热情 提交于 2020-01-07 02:27:46
问题 In continuation of this topic Variadic template heterogeneous container, I would like to ask the following. Assume, that we have several classes with two members that is dynamic arrays. Now suppose that there is a sequence of objects of these classes, which is packed in heterogeneous container. In this sequence one of arrays-mebers is "output" vector and another array-member is "input" vector, which is pointer to appropriate output array from preceding object. This sequence is implemented as

How to implement data execution (as if it were script source code) [closed]

[亡魂溺海] 提交于 2020-01-06 14:10:46
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Suppose there is a script language called ScriptCode with the capabilities of execute code in same language. //This ficticius program executes a simple constant code. main() { ScriptCode sc=new ScriptCode ( "print \"Hello\""); execute(sc); print (" world"); } //This ficticius program would read 10