几个月前我一直在使用一些红宝石库(我不记得哪一个,不幸的是)
我很惊讶地看到它允许我用这样的东西初始化它的实例:
Lib::SOMETHING(args)
Run Code Online (Sandbox Code Playgroud)
我真的不明白它是如何工作的.我非常确定它应该是动态的东西(没有SOMETHING常量),比如constant_missing模块方法,或者可能ConstantMissing以某种方式处理异常.
你能建议吗?
我想做的就是这样
def mult(x, y):
return x * y
def add(x, y):
return x + y
treeFunction = mult(2, add(x, y))
#outputs 10 (2 * ( 2 + 3 ))
print treeFunction(2, 3)
Run Code Online (Sandbox Code Playgroud)
基本上,在运行时有没有办法通过将add()的值乘以某个值来改变它的返回值.
我知道def是一个关键字,不能被覆盖.
但是有一种方法可以在方法向类注册时调用方法(传入正在创建的方法的名称吗?)
typedef std::tuple< int, double > Tuple;
Tuple t;
int a = std::get<0>(t);
double b = std::get<1>(t);
for( size_t i = 0; i < std::tuple_size<Tuple>::value; i++ ) {
std::tuple_element<i,Tuple>::type v = std::get<i>(t);// will not compile because i must be known at compile time
}
Run Code Online (Sandbox Code Playgroud)
我知道可以编写代码来实现std::get工作(参见例如迭代元组),是否可以开始std::tuple_element工作?
一些约束(它们可以放松):
没有可变参数模板,没有Boost
我有一个Ruby程序,它生成包含Module定义的文件:
# try.rb
module Test
class F1
def initialize arg
@arg=arg
end
end
end
Run Code Online (Sandbox Code Playgroud)
现在,我想用相同的程序来创建F1的实例.
这样做的成语是什么?
我有一个引用方法的变量,我用eval关键字调用该方法
a_test = "myvariable"
eval a_test
def myvariable
(...)
end
Run Code Online (Sandbox Code Playgroud)
我想将一个变量传递给方法,例如
def myvariable(var1)
(...)
end
Run Code Online (Sandbox Code Playgroud)
是否有人熟悉任何"成语"的方式来实现这一目标.做点什么
eval a_test "string_test"
Run Code Online (Sandbox Code Playgroud)
自然会失败,因为解释器将对名为"a_test"的函数进行查找
我正在创建一个名为Configuration的模型,我有以下代码,我想通过使用元编程使其更具动态性.
在配置模型的数据库表中,我有以下数据.
---------------------------------------------------------
variable_name as string | value in text
|
company_name | MyCompany
welcome_text | Welcome to MyCompany's App!
email_order_text | You've just created an account with MyCompany.
year_since | 2012
----------------------------------------------------------
class Configuration < ActiveRecord::Base
#nothing here yet
end
----------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
目前,访问company_name的唯一方法是在rails控制台中执行以下操作:
configuration_company_name = Configuration.find_by_variable_name("company_name")
configuration_company_name.company_name
> "MyCompany"
Run Code Online (Sandbox Code Playgroud)
我认为这是一种不可接受的做事方式.首先,每次有人检查公司名称时,它都会访问数据库.我想如果我可以在应用程序启动时加载它并且不必再次访问它,因为它在内存中,那么它会更好.我怎样才能做更动态的事情,所以我可以像这样访问"MyCompany"这个值.
Configuration.company_name
> "MyCompany"
Run Code Online (Sandbox Code Playgroud)
这样做的原因是允许快速定制应用程序.
>>> def test(arg1, arg2, arg3=None): pass
>>> test.func_code.co_varnames
('arg1', 'arg2, 'arg3')
Run Code Online (Sandbox Code Playgroud)
我想要的是:
{'args': ('arg1', 'arg2'), 'kwargs': {'arg3': None}}
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
我正在使用Ruby 1.9.2和Ruby on Rails v3.2.2 gem.我正在尝试以"正确的方式"学习元编程,此时我在RoR 模块提供的块中对实例方法进行别名:included do ... endActiveSupport::Concern
module MyModule
extend ActiveSupport::Concern
included do
# Builds the instance method name.
my_method_name = build_method_name.to_sym # => :my_method
# Defines the :my_method instance method in the including class of MyModule.
define_singleton_method(my_method_name) do |*args|
# ...
end
# Aliases the :my_method instance method in the including class of MyModule.
singleton_class = class << self; self end
singleton_class.send(:alias_method, :my_new_method, my_method_name)
end
end
Run Code Online (Sandbox Code Playgroud)
"Newbiely"说,通过在Web上搜索,我想出了singleton_class = class << self; …
我试着编写struct Fraction它在编译时执行基本操作.请注意,这不是任何真正的目的 - 我这只是一个练习.
我从这开始:
namespace internal
{
// Euclid algorithm
template <int A, int B>
struct gcd
{
static int const value = gcd<B, A % B>::value;
};
// Specialization to terminate recursion
template <int A>
struct gcd<A, 0>
{
static int const value = A;
};
}
template <int Numerator, int Denominator>
struct Fraction
{
// Numerator and denominator simplified
static int const numerator = Numerator / internal::gcd<Numerator, Denominator>::value;
static int const denominator = Denominator / …Run Code Online (Sandbox Code Playgroud) metaprogramming ×10
ruby ×6
c++ ×2
python ×2
c++11 ×1
definition ×1
eval ×1
function ×1
idioms ×1
methods ×1
model ×1
python-2.7 ×1