metaprogramming

creating dynamic helper methods in rails

亡梦爱人 提交于 2019-12-24 18:20:50
问题 I am trying to create a bunch of dynamic helper methods like these: show_admin_sidebar show_posts_sidebar show_users_sidebar So far I have this in my helper.rb file: #spits out a partial def show_sidebar(name, show_sidebar = true) @content_for_sidebar = render :partial => "partials/#{name}" @show_sidebar = show_sidebar end def show_sidebar? @show_sidebar end In my application layout file I have this: (NB - I'm using HAML): - if show_sidebar? = yield(:sidebar) This allows me to say the

C++ compile time check if type has mutable fields

杀马特。学长 韩版系。学妹 提交于 2019-12-24 16:49:20
问题 All is it possible to get true/false flag for class having mutable field? Could I distinguish during compilation between struct A { private: mutable int _x; }; and struct B { private: int _x; }; More complex question, is it possible to do so NOT knowing name of the field? 来源: https://stackoverflow.com/questions/35519596/c-compile-time-check-if-type-has-mutable-fields

Ruby failing to override send method

白昼怎懂夜的黑 提交于 2019-12-24 13:34:08
问题 This is a bit hard to explain, but I seem to be occasionally unable to override the 'send' method in my application. I'm creating a fairly large application based on EventMachine and sometimes, deep in the bowels of my code, I decide to define a 'send' method in one of my classes. When I later attempt to use this method, I normally get an exception that looks something like TypeError: <parameter> is not a symbol , such as the following (happens to be caused by the required AMQP gem (not mine)

Work with unique_ptr<int[]>, vector<int>, and int[] in a Templatized Function

こ雲淡風輕ζ 提交于 2019-12-24 12:06:10
问题 Say that I have 3 variables: vector<int> vec(3); int stat[3]; auto dyn = make_unique<int[]>(3); I can initialize any of these if I know the size is 3: for(auto i = 0; i < 3; ++i) X[3] = i; Where X is either, vec , stat , or dyn . But I'd like to be able to do this in a template just by passing in X again. What I'd need in order to do this is: The contained type The container size Can I get that in a function like: template <typename T> void init(T& X); Or am I unable to extract size

Detecting infinite recursion

江枫思渺然 提交于 2019-12-24 06:28:14
问题 I'm creating a macro for Trac, and one of the things it does is to render a bit of wiki text, that can in turn use the same macro. This can give rise to an infinite recursion if the inner macro is invoked with the same arguments (i.e., renders the same bit of wiki text). I thought of trying to stop the user from shooting his own foot like this by inspecting the call stack and breaking the recursion if the function that expands the macro was already invoked with exactly the same set of

How do I forward declare a template type that has been forward declared elsewhere with a defaulting

六眼飞鱼酱① 提交于 2019-12-24 03:56:07
问题 So the excellent answer to this question states that you can default template types in a forward declaration, however: You can specify each default template argument only once This is more fully specified in the linked question, but Boost's Property Tree stuff works with the ptree type: typedef basic_ptree<std::string, std::string> ptree; But the basic_ptree class is defined like this: template<class Key, class Data, class KeyCompare> class basic_ptree The only reason the ptree typedef is

How to develop MS CRM kind of application

对着背影说爱祢 提交于 2019-12-24 03:48:18
问题 I have worked with MS CRM. There we can design our custom entity graphically and then we can also build a visual form to perform CRUD operations on that entity. This feels so simple from end user's perspective. However I am interested to know how can I develop the similar kind of application where I design my table on the fly and the design UI on the fly. What I want to know is like how do they achieve all of this dynamically? If I have to create CRUD on one simple table, I need to write good

How to enhance attr_accessor in ruby?

断了今生、忘了曾经 提交于 2019-12-24 02:23:31
问题 I want to implement a (class) method attr_accessor_with_client_reset , which does the same thing as attr_accessor , but on every writer it additionally executes @client = nil So, for example, attr_accessor_with_client_reset :foo should produce the same result as attr_reader :foo def foo=(value) @foo = value @client = nil end How do I achieve this? 回答1: Sergio's solution is good, but needlessly complex: there's no need to duplicate the behavior of attr_reader , you can just delegate to it. And

Groovy meta-class to intercept interactions between Java objects

霸气de小男生 提交于 2019-12-24 02:23:10
问题 Main issue: It seems that if I modify a Java class's metaClass then such changes are only honored if my Groovy code is invoking that Java class. However if another Java class is invoking the Java class that I modified, then the metaClass changes are ignored. I guess this behavior seems to be proper (the Java code that was compiled will not check the Groovy meta-class registry) but I was hoping someone can give me some other ideas on how to achieve my goals. Here's some sample code: A.java :

How can I use functools.partial on multiple methods on an object, and freeze parameters out of order?

守給你的承諾、 提交于 2019-12-24 01:38:19
问题 I find functools.partial to be extremely useful, but I would like to be able to freeze arguments out of order (the argument you want to freeze is not always the first one) and I'd like to be able to apply it to several methods on a class at once, to make a proxy object that has the same methods as the underlying object except with some of its methods parameters being frozen (think of it as generalizing partial to apply to classes). And I'd prefer to do this without editing the original object