可能重复:
使用模板获取数组的大小和结束地址有人可以解释这个模板代码,它给我一个数组的大小?(第一个答案包括获取值作为编译时间常量)
如何使用元编程获得数组的大小?多维也将受到赞赏.所以例如,如果我将一个类型传递给这个结构(如何调用它,让我们说get_dim)我会得到:
get_dim<int>::value; //0
get_dim<int[1]>::value //1
get_dim<int[2][3]>::value //2,3
Run Code Online (Sandbox Code Playgroud) 研究了一些template程序,特别是在编译时将结果推导成常量的元程序,我了解到通常只有一种方法可以实现某些东西.例如:像factorial示例一样简单,或者像is_base_of一样复杂
我永远无法考虑具有完全不同逻辑的此类代码的完美替代实现.这是一个真实的假设吗?
如果这个假设是真的意味着,每当我们使用模板技巧实现某些东西时,我们总是确信,它是最好的代码,我们不必再担心优化编译时间了.
[注意:我没有提到我们template使用的一般用法class和功能.但用于推断编译时常量的用法.
我看到如何动态地向Ruby中的实例添加方法def [instance].[methodname]; [...]; end.
但是,我有兴趣将另一个位置中存在的方法附加到给定实例.例如
def my_meth
puts self.foo
end
class MyCls
attr_accessor :foo
end
my_obj = MyCls.new
my_obj.my_meth
Run Code Online (Sandbox Code Playgroud)
我怎么能简单地附加my_meth到my_obj上面的代码的最后一行中的方法调用?
伙计们!我需要创建某种元语言,我可以将其嵌入XML中,然后用Java进行解析.例如:
<code>
[if value1>value2 then "Hello, Bob!" else "Hello, Jack"]
</code>
Run Code Online (Sandbox Code Playgroud)
要么
<code>
[if value1+2>value2 return true]
</code>
Run Code Online (Sandbox Code Playgroud)
我需要实现条件语句,算术.
我应该从哪里开始寻找建议?
说我有一些CoffeeScript中(与Underscore.js混合)是这样的:
someData =
hello: 'haha'
_(3).times (index) ->
someData["key-#{index}"] = index
Run Code Online (Sandbox Code Playgroud)
someData那么价值将是:
hello: 'haha'
key-0: 0
key-1: 1
key-2: 2
Run Code Online (Sandbox Code Playgroud)
如果Coffeescript有一些语法糖允许我写这样的东西会很好:
someData =
hello: 'haha'
<%
_(3).times (index) ->
%>
key-#{index}: index
Run Code Online (Sandbox Code Playgroud)
这将产生someData其价值与原始价值相同的价值.
Coffeescript有这样的设施吗?
我有以下类覆盖:
class Numeric
@@currencies = {:dollar => 1, :yen => 0.013, :euro => 1.292, :rupee => 0.019}
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '').to_sym
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
end
end
def in(destination_currency)
destination_curreny = destination_currency.to_s.gsub(/s$/, '').to_sym
if @@currencies.has_key?(destination_currency)
self / @@currencies[destination_currency]
else
super
end
end
end
Run Code Online (Sandbox Code Playgroud)
每当in的参数为复数时,例如:10.dollars.in(:yens)我得到ArgumentError: wrong number of arguments (2 for 1)但不10.dollars.in(:yen)产生错误.知道为什么吗?
我的代码中似乎有一个错误.但是我无法找到它.
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
attr_reader attr_name
attr_writer attr_name
attr_reader attr_name + "_history"
class_eval %Q{
@#{attr_name}_history=[1,2,3]
}
end
end
class Foo
attr_accessor_with_history :bar
end
f = Foo.new
f.bar = 1
f.bar = 2
puts f.bar_history.to_s
Run Code Online (Sandbox Code Playgroud)
我希望它能返回一个数组[1,2,3].但是,它不返回任何东西.
我的C++代码中有一个奇怪的元函数行为,我想了解原因.
#include <iostream>
#include <cmath>
inline double f(double x, double y)
{
std::cout<<"Marker"<<std::endl;
return sqrt(x*y);
}
template <int N, class T> inline T metaPow(T x)
{
return ((N > 0) ? (x*metaPow<((N > 0) ? (N-1) : (0))>(x)) : (1.));
}
int main()
{
double x;
double y;
std::cin>>x;
std::cin>>y;
std::cout<<metaPow<5>(f(x, y))<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我预计这条线metaPow<5>(f(x, y))相当于f(x, y)*f(x, y)*f(x, y)*f(x, y)*f(x, y)*1..但如果是的话,它会打印Marker出f函数中" "行的五倍.
奇怪的是,我最终得到了很好的结果(例如181.019for x = 2和y = …
是否有更简单和/或更易读的方法在Ruby中创建闭包,以便定义的方法可以访问变量 m?
我在lambda这里有一个轻微的"问题" .
我经常动态定义必须访问局部变量的方法:
例如:
class Comparison
def income
123
end
def sales
42342
end
# and a dozen of other methods
# Generate xxx_after_tax for each method
instance_methods(false).each do |m|
lambda {
define_method("#{m}_after_tax") do
send(m) * 0.9
end
}.call
end
end
Run Code Online (Sandbox Code Playgroud) 在Rails中,我有一个类名User,我只想看一下:name, :address, :age
我想写一段代码,如下所示:
user = User.new
[name, address, age].zip(["Name", "Address", 10]).each do |attribute, val|
user.attribute = val
end
Run Code Online (Sandbox Code Playgroud)
事情是我不知道如何正确地做,因为user.attribute显然不是一个有效的线.换句话说,反正是有,这样user.attribute得到的评价user.name,user.address,user.age取决于环路?
谢谢
metaprogramming ×10
ruby ×5
c++ ×3
attributes ×1
closures ×1
coffeescript ×1
java ×1
numeric ×1
optimization ×1
pow ×1
templates ×1