metaprogramming

Does powershell have a method_missing()?

时间秒杀一切 提交于 2020-01-04 02:16:11
问题 I have been playing around with the dynamic abilities of powershell and I was wondering something Is there is anything in powershell analogous to Ruby's method_missing() where you can set up a 'catch all method' to dynamically handle calls to non-existant methods on your objects? 回答1: No, not really. I suspect that the next version of PowerShell will become more in line with the dynamic dispatch capabilities added to .NET 4 but for the time being, this would not be possible in pure PowerShell

C++ Tuple of Boost.Range - get Tuple of element types?

那年仲夏 提交于 2020-01-03 17:48:10
问题 I am experimenting with Boost.Range and the Boost Tuple. If I have a Tuple of ranges, how can I typedef a Tuple or the corresponding element values? To put this another way, what do I put in place of /*?*/ here: typedef boost::tuples::tuple<std::vector<int>&, char[]> TupleOfRanges; typedef /*?*/ TupleOfElements; I can do this by hand, of course and I would write: typedef boost::tuples::tuple<int, char> TupleOfElements; Or even: typedef typename boost::tuples::element<0, TupleOfRanges>::type

How to use Ruby's metaprogramming to reduce method count

冷暖自知 提交于 2020-01-03 17:15:08
问题 I have a bunch of methods that are repeating, and I am sure I can use Ruby's metaprogramming somehow. My class looks like this: class SomePatterns def address_key "..." end def user_key "..." end def location_key "..." end def address_count redis.scard("#{address_key}") end def location_count redis.scard("#{location_key}") end def user_count redis.scard("#{user_key}") end end I was thinking I could have only one method like: def count(prefix) redis.scard("#{prefix}_key") # wrong, but

Create a tuple with variatic type wrapped

时间秒杀一切 提交于 2020-01-03 17:08:49
问题 Today I'm trying to create a tuple a little specific (for me at least) and at compile time. I have some basic struct, let say : struct Foo1 { int data; }; struct Foo2 { int data; }; struct Foo3 { int data; }; And another struct but with some template stuff : template < typename T, size_t Size > struct Metadata { using type = T; std::bitset<Size> bitset; }; So now I want to create this kind of tuple : constexpr std::tuple<Metadata<Foo1, 3>, Metadata<Foo2, 3>, Metadata<Foo3, 3>> test { {0}, {0}

Lisp / Clojure: Is it a good idea to write function generating macros?

旧街凉风 提交于 2020-01-03 09:25:07
问题 This question asks to create a Clojure macro to generate several functions. We figured out a way to do this but were stuck with the question of "Is this a good idea?". My initial reaction is not really , for two reasons You then have functions that are not defined in your code, and this can complicate understanding your code quite a bit! (Imagine somebody has a problem with one of your functions and looks at the source code only to not find it anywhere). It is better to factor out the

Python: Adding Fields to Objects Dynamically

主宰稳场 提交于 2020-01-03 07:25:10
问题 I am wondering whether it is possible to add fields to objects dynamically. For example, I want to be able to add something like: user = object() user.first_name = 'John' user.last_name = 'Smith' When I execute that in Python command line interpretor I get: AttributeError: 'object' object has no attribute 'first_name' Any idea? 回答1: Try this: class Object: pass obj = Object() obj.x = 5 回答2: You cannot assign to attributes of object instances like this. Derive from object , and use an instance

Does Ruby have a method_missing equivalent for undefined instance variables?

拟墨画扇 提交于 2020-01-03 07:19:10
问题 When I invoke a method that doesn't exist, method_missing will tell me the name of the method. When I attempt to access a variable that hasn't been set, the value is simply nil . I'm attempting to dynamically intercept access to nil instance variables and return a value based on the name of the variable being accessed. The closest equivalent would be PHP's __get. Is there any equivalent functionality in Ruby? 回答1: I do not believe this is possible in Ruby. The recommended way would be to use

Symfony2 Assert\Expression annotation doesn't support Constants

非 Y 不嫁゛ 提交于 2020-01-02 09:58:07
问题 Generally I use Constants in all of annotation based places e.g. annotations, route and assert annotations , but in Assert\Expression it throws Variable "EntityInterface" is not valid around position 26. Is this a bug or is a special rare case ? <?php /** * @var string * * @ORM\Column(name="id_number", type="string", length=11, nullable=true) * @Assert\Expression( * "this.getNationality() == EntityInterface::COUNTRY_DEFAULT_VALUE and value != null", * message = "form.user.validation.id_number

Symfony2 Assert\Expression annotation doesn't support Constants

℡╲_俬逩灬. 提交于 2020-01-02 09:57:03
问题 Generally I use Constants in all of annotation based places e.g. annotations, route and assert annotations , but in Assert\Expression it throws Variable "EntityInterface" is not valid around position 26. Is this a bug or is a special rare case ? <?php /** * @var string * * @ORM\Column(name="id_number", type="string", length=11, nullable=true) * @Assert\Expression( * "this.getNationality() == EntityInterface::COUNTRY_DEFAULT_VALUE and value != null", * message = "form.user.validation.id_number

Classify a Ruby string

ぐ巨炮叔叔 提交于 2020-01-02 09:28:26
问题 I have a program that create classes which looks like: MyClass = Class.new do def initialize; end # ... end But I would like to name dynamically MyClass, from a string. And because it's for the name of a class, I would like to classify that string, for instance (thanks Rails methods): "hello_world".classify # => "HelloWorld" I don't know if in pure Ruby there is a method for that. Thank you 回答1: Not sure if your question is only about constructing a camelcased string, or also about assigning