metaprogramming

Ruby metaprogramming: cannot send a method to a module

╄→尐↘猪︶ㄣ 提交于 2019-12-24 00:55:08
问题 For example I have following custom class and module: module SimpleModule def hello_world puts 'i am a SimpleModule method' end def self.class_hello_world puts 'i am a SimpleModule class method' end end class SimpleClass def hello_world puts 'i am SimpleClass method' end def self.class_hello_world puts 'i am a SimpleClass class method' end end I tried to called those methods inside class and module by using method send SimpleClass.send(class_hello_world) # work SimpleClass.new.send(hello

Yield in class << self in class method

最后都变了- 提交于 2019-12-24 00:47:45
问题 I want to create a class method that takes a block of method definitions and injects it into the class. Now, self is indeed the Tom object so class << self does open it up, but yield doesn't seem to work. My theoretical knowledge is not that deep, so I am not sure why this is not working. I might be getting this totally wrong, so feel free to discuss alternatives. class Tom < Person mega_methods do def hiya! puts 'hiYA!' end end end class Person def self.mega_methods ... class << self yield

array of 2s power using template in c++

萝らか妹 提交于 2019-12-24 00:18:36
问题 Can we create the following array or something similar using template in c++ at compile time. int powerOf2[] = {1,2,4,8,16,32,64,128,256} This is the closest I got. template <int Y> struct PowerArray{enum { value=2* PowerArray<Y-1>::value };}; but then to use I need something like PowerArray <i> which compiler gives error as i is dynamic variable. 回答1: You can use BOOST_PP_ENUM for this: #include <iostream> #include <cmath> #include <boost/preprocessor/repetition/enum.hpp> #define ORDER(z, n,

Infix format for Nemerle macro

孤街浪徒 提交于 2019-12-23 22:37:51
问题 Say I need some very special multiplication operator. It may be implemented in following macro: macro @<<!(op1, op2) { <[ ( $op1 * $op2 ) ]> } And I can use it like def val = 2 <<! 3 And its work. But what I really want is some 'english'-like operator for the DSL Im developing now: macro @multiply(op1, op2) { <[ ( $op1 * $op2 ) ]> } and if I try to use it like def val = 2 multiply 3 compiler fails with 'expected ;' error What is the problem? How can I implement this infix-format macro? 回答1:

Define method parameters for meta programming in Ruby

不打扰是莪最后的温柔 提交于 2019-12-23 22:36:28
问题 In Ruby, we can define instance methods with meta-programming like: define_method(:hi) { 'Hello, SO world!' } # => :hi hi # => "Hello, SO world!" This way, it is possible to define a method with a dynamic name : dynamic_name = :hi # => :hi define_method(dynamic_name) { 'Hello, SO world!' } # => :hi hi # => "Hello, SO world!" We can also inject arguments in to a dynamic method: dynamic_name = :hi # => :hi define_method(dynamic_name) do |arg1, arg2, &block| 'Hello, SO world!' end # => :hi hi(42

How to automatically infer the return type from a function type?

谁都会走 提交于 2019-12-23 20:55:00
问题 I'm using boost::python to create a Python wrapper of a C++ library. At some point, boost::python needs a pointer to a member function (or something compatible), like: template <class MyClass, typename ValueType> void (*setter_function)(MyClass&, ValueType) // This doesn't compile, but you got the idea. my_boost_python_call(setter_function f); Since the class I'm wrapping has its setter in the following form: template <class MyClass, typename ValueType> MyClass& (MyClass::*setter_method)

Implementing the (typed) K combinator in C++

爷,独闯天下 提交于 2019-12-23 18:27:11
问题 I am trying to implement the K combinator from the SK combinator calculus in C++. The K combinator is a higher-order function that basically takes some value x , and returns something which in turn takes a value y and returns x from it. In other words, K(x)(y) == x or step-by-step: intermediate = K(x) intermediate(y) == x The ability to treat K(x) as a thing-in-itself, independent of y , is essential. Furthermore, it should not be necessary to specify the type of y when simply creating K(x)

Can an interface be retroactively implemented in VB.NET?

点点圈 提交于 2019-12-23 18:26:24
问题 Is it possible to define an interface (e.g. MyClass Implements MyInterface) whose method/property definitions already match some of the methods/properties defined on a third-party (or a native) class? For example, the DataRow class has a number of properties/methods that make it "row-like". What if I want to implement an interface (i.e. IRowLike) that defines certain methods and properties already existing on the native DataRow class (which I cannot directly touch or extend). I simply want

Overriding private method with metaClass in Groovy

扶醉桌前 提交于 2019-12-23 18:17:47
问题 This snippet used to work properly with Groovy 2.1.0: class User { private String sayHello() { return "hello" } } assert new User().sayHello() == "hello" User.metaClass.sayHello = { return "goodbye" } assert new User().sayHello() == "goodbye" but it does not work anymore in Groovy 2.4.3. Does anybody know how to override the behaviour of a private method with Groovy (if possible)? 来源: https://stackoverflow.com/questions/31938551/overriding-private-method-with-metaclass-in-groovy

Defining a new logical operator in Ruby

我的梦境 提交于 2019-12-23 09:56:24
问题 Very much an idle day-dream this but is it possible with some neat meta-programming trick to define a new logical operator in Ruby? I'd like to define a but operator. For example, if I want to do something if x but not y is true I have to write something like: if x and not y But I would like to write if x but not y It should work exactly the same as and but would be down to the programmer to use sensibly to increase the legibility of code. 回答1: Without editing the Ruby parser and sources and