在Ruby中,是否可以从超类方法转换为子类方法而又不影响子类中的代码?我试图避免super在子类中调用。
class SuperClass
def do_something
puts "getting ready..."
# how to then yield back to the subclass do_something method?
puts "done."
end
end
class SubClass < SuperClass
def do_something
# how to first execute the superclass do_something method?
puts "doing something ..."
end
end
Run Code Online (Sandbox Code Playgroud)
所需的功能是专门调用SubClass.do_something并接收以下输出:
“准备...”
“做某事...”
“做完了。”
编辑:
也许真正的问题是:如何干涸下面的代码,删除对来电self.get_ready和self.finish_up所有子类,使用任何红宝石的元编程技术,保持这些类DRY:
class SuperClass
def self.get_ready
puts "getting ready ..."
end
def self.finish_up
puts "done."
end
end
class SubClassA < SuperClass
def self.do_something
self.get_ready …Run Code Online (Sandbox Code Playgroud) 在我的程序中,我使用状态机,并有许多方便的方法.我目前正在创建一个很长的"?"列表 模型中的方法.
def purchase_ready?
self.current_state == 'purchase_ready'
end
def completed?
self.current_state == 'completed'
end
def region_prepared?
self.current_state == 'region_prepared'
end
Run Code Online (Sandbox Code Playgroud)
什么是元编程方式呢?
让我们假设我们有两个类
struct A
{
int x = 1;
};
struct B
{
int y = 2;
};
Run Code Online (Sandbox Code Playgroud)
我想要有返回成员值的模板(在AI的情况下想要返回"x"的值,如果BI想要返回"y"的值).
示例电话:
const auto myVariable = f<A>();
Run Code Online (Sandbox Code Playgroud)
要么
A a;
const auto myVariable = f<A>(a);
Run Code Online (Sandbox Code Playgroud)
我不想有2个模板专业化 - 理想情况下它会是一个带有某种"if语句"的模板,但也许它不可能?
它可以用C++ 11编写(但不能用C++ 14编写).
一般来说,当你遇到这样的问题时如何使用模板 - 相当大的模板,只需要在一两个地方你需要从不同的成员中获取值 - 这可以根据该变量的类型推断出来.
问题:不必要的是不允许修改A类和B类
如何使用元类在python中生成属性?我有一些数据记录,哪些字段之间有某些关系。我想将每个记录都作为类型(类)并自动生成这些属性和关系。
我想将此关系指定为数据结构(例如dict),并自动生成具有属性的类
Record 1
Field description
----------------------------
numOfPin no# of following pin array
pin_array array with numOfpin elements
read_result if opt_flag bit 0 is set, invalid. ignore this value
opt_flag ...
Record 2
....
Run Code Online (Sandbox Code Playgroud)
编辑:进行澄清。缺少/无效字段和可选标志的属性的逻辑对于所有记录都是相同的。因此,我想在元类中对此进行抽象。
例如在伪python代码中:
record1_spec = { 'numOfpin', ('pin_array','numOfPin'),
'opt_flag', ('read_result','opt_flag') }
record2_spec = { 'numOfsite', ('site_array','numOfsite'),
'opt_flag', ('test_result','opt_flag') }
class MetaClass:
getter_generator( ele, opt ):
if opt : return ele
else return None
get class name (eg. record1) then fetch record_spec to create class
for ele in …Run Code Online (Sandbox Code Playgroud) 我定义了一个这样的函数,其中有一个模板模板类
template<typename Key, typename Value, template <typename, typename> class Map>
struct ForEachOf {
void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) {
for(const auto& pair : map) {
func(pair.first, pair.second);
}
}
};
std::map<int, string> m { {1, "foo"}, {3, "bar"}};
ForEachOf<int, string, std::map> forEachOf;
forEachOf(m, [](int key, string value) {
cout << key << value;
});
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码无法编译.错误就像:
error: template template argument has different template parameters
than its corresponding template template parameter
ForEachOf<int, string, std::map> forEachOf;
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1119:5: note: too …Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming template-meta-programming c++11
我最近一直在阅读Metaprogrammin Ruby第二版,在第5章的最后,他们提供了一个小测验,
你的任务是更改
Fixnum类,以便答案1+1变为3,而不是2.
我理解教科书中的解决方案没有太多麻烦(他们Fixnum直接重新开课).但我想尽可能地尝试将独立方法分离到一个独立的方法Module.类似下面的东西.
但是在下面运行会导致新定义的内容无限+.你能指出这段代码有什么问题吗?先感谢您.
module PlusOneMore
def self.prepended(base)
base.class_eval{
alias_method :original_plus, :+
}
end
def +(n)
original_plus(n).original_plus(1)
end
end
Fixnum.class_eval do
prepend PlusOneMore
end
puts 1.+(1)
Run Code Online (Sandbox Code Playgroud) 这是一个类似于如何在每次方法调用后隐式调用方法的问题?但是对于python
假设我有一个带有一些属性的爬虫类(例如self.db)crawl_1(self, *args, **kwargs)和另一个save_to_db(self, *args, **kwargs)用于将爬行结果保存到数据库的属性(self.db).
我想以某种方式save_to_db在每次crawl_1, crawl_2, etc.通话后都跑.我已经尝试将其作为"全局"的util装饰器,但我不喜欢结果,因为它涉及self作为参数传递.
python metaprogramming decorator python-3.x python-decorators
鉴于以下内容:
for fn_name <- [:foo, :bar, :baz] do
defmacro unquote(fn_name)(do: inner) do
fn_name = unquote(fn_name) # <--- Why?
quote do
IO.puts "#{unquote(fn_name)} called"
unquote(inner)
end
end
end
Run Code Online (Sandbox Code Playgroud)
是什么原因fn_name = unquote(fn_name)?如果我省略这一行,那就是编译错误.这种"双重"不引用的原因是什么?
我正在编写一个基于Python的列表推导的宏@vcomp(矢量理解)和一个条件子句,以简洁的方式过滤元素.
macro vcomp(comprehension::Expr, when::Symbol, condition)
comp_head, comp_args = comprehension.head, comprehension.args
comp_head ? [:comprehension, :typed_comprehension] && error("@vcomp not a comprehension")
when ? :when && error("@vcomp expected `when`, got: `$when`")
T = comp_head == :typed_comprehension ? comp_args[1] : nothing
if VERSION < v"0.5-"
element = comp_head == :comprehension ? comp_args[1] : comp_args[2]
sequence = comp_head == :comprehension ? comp_args[2] : comp_args[3]
else
element = comp_head == :comprehension ? comp_args[1].args[1] : comp_args[2].args[1]
sequence = comp_head == :comprehension ? comp_args[1].args[2] …Run Code Online (Sandbox Code Playgroud) 我需要使用innerHTML快速更改在每个节点上设置的值。
我找到的最接近的解决方案是:
...
Object.defineProperty(Element.prototype, 'innerHTML', {
set: function () {
// get value (ok)
var value = arguments[0];
// change it (ok)
var new_value = my_function(value);
// set it (problem)
this.innerHTML = new_value; // LOOP
}
}
...
Run Code Online (Sandbox Code Playgroud)
但这显然是一个无限循环。有没有一种方法可以调用原始的innerHTML集?
我也尝试使用代理方式,但无法使其正常工作。
更多细节:
我正在一个实验项目中,该项目使用反向代理来生成CSP策略并将其添加到网站,因此:
javascript hook metaprogramming innerhtml content-security-policy
metaprogramming ×10
ruby ×3
c++ ×2
c++11 ×2
python ×2
python-3.x ×2
templates ×2
decorator ×1
elixir ×1
hook ×1
inheritance ×1
innerhtml ×1
javascript ×1
julia ×1
macros ×1
metaclass ×1