我正在Ruby 1.9.2中编写一个定义了几个方法的模块.当调用这些方法中的任何一个时,我希望它们中的每一个首先执行某个语句.
module MyModule
def go_forth
a re-used statement
# code particular to this method follows ...
end
def and_multiply
a re-used statement
# then something completely different ...
end
end
Run Code Online (Sandbox Code Playgroud)
但我想避免a re-used statement在每个方法中明确地放置该代码.有办法吗?
(如果重要的话,a re-used statement每个方法在调用时都会打印自己的名字.它会通过一些变体来实现puts __method__.)
我在Ruby中使用DSL,其工作原理如下:
desc 'list all todos'
command :list do |c|
c.desc 'show todos in long form'
c.switch :l
c.action do |global,option,args|
# some code that's not relevant to this question
end
end
desc 'make a new todo'
command :new do |c|
# etc.
end
Run Code Online (Sandbox Code Playgroud)
一位开发人员建议我将DSL增强到不需要传递c给command块,因此不需要c.内部的所有方法; 据推测,他暗示我可以使下面的代码工作相同:
desc 'list all todos'
command :list do
desc 'show todos in long form'
switch :l
action do |global,option,args|
# some code that's not relevant to this question
end
end …Run Code Online (Sandbox Code Playgroud) 在纯C++世界中,我们可以在编译时使用基于模板的编译时和运行时技术的组合在不同组件或接口之间生成接口或粘合代码(例如,大多数自动编组到使用传统类型的调用).
当必须使用Objective-C/Cocoa将C++应用程序与GUI,系统集成或IPC接口时,由于不太严格的类型,事情变得更加困难 - 但通常不需要更多的平面复制接口层:瘦桥接代表必须是必须编写定义或转换代码到语言桥接调用.
如果你必须处理非平凡大小的接口并希望避免基于脚本的代码生成,这很快变得麻烦,并且每次重构都必须发生时只是痛苦.使用(模板)元编程和Objective-C运行时库的组合,应该可以大大减少代码量...
在我重新发明轮子之前(可能浪费时间),有没有人知道这方面的技术,最佳实践或例子?
举个例子,假设我们需要一个支持这种非正式协议的委托:
- (NSString*)concatString:(NSString*)s1 withString:(NSString*)s2;
- (NSNumber*) indexOf:(CustomClass*)obj;
Run Code Online (Sandbox Code Playgroud)
现在,我没有实现明确桥接到C++实例的Obj-C类,而是想做类似的事情:
class CppObj {
ObjcDelegate m_del;
public:
CppObj() : m_del(this)
{
m_del.addHandler
<NSString* (NSString*, NSString*)>
("concatString", &CppObj::concat);
m_del.addHandler
<NSNumber* (CustomClass*)>
("indexOf", &CppObj::indexOf);
}
std::string concat(const std::string& s1, const std::string& s2) {
return s1.append(s2);
}
size_t indexOf(const ConvertedCustomClass& obj) {
return 42;
}
};
Run Code Online (Sandbox Code Playgroud)
用户需要支持其他类型的所有内容都是专门化转换模板功能:
template<class To, class From> To convert(const From&);
template<>
NSString* convert<NSString*, std::string>(const std::string& s) {
// ...
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个新类,它将继承自ActiveRecord::Base需要从字符串动态生成的类
"general_systems".camelize.singularize = Class.new < ActiveRecord::Base
Run Code Online (Sandbox Code Playgroud)
但是我一直收到错误:
undefined method `singularize=' for "GeneralSystems":String
Run Code Online (Sandbox Code Playgroud)
我也尝试constantize过字符串
>> foo = "general_systems".camelize.singularize
=> "GeneralSystem"
>> foo.constantize
NameError: uninitialized constant GeneralSystem
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:124:in `block in constantize'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `each'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `constantize'
from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/string/inflections.rb:43:in `constantize'
from (irb):4
from /usr/bin/irb:12:in `<main>'
>> foo.constantize = Class.new
NoMethodError: undefined method `constantize=' for "GeneralSystem":String
from (irb):5
from /usr/bin/irb:12:in `<main>'
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我想知道elixir中的方法名称究竟是什么:
array = [1,2,3]
module_name = :lists
method_name = :nth # this not working
module_name.method_name(1, array) # error, undef function lists.method_name/2
module_name.nth(1, array) # returns 1, module_name is OK. It's an atom
Run Code Online (Sandbox Code Playgroud)
但我可以在erlang中做同样的事情:
A = [1,2,3].
X = lists.
Y = nth.
X:Y(1,A). # returns 1
Run Code Online (Sandbox Code Playgroud)
我怎么能在灵药中做到这一点?
我想从被调用的函数中检索Python中的局部变量.有没有办法做到这一点?我意识到这不适合大多数编程,但我基本上构建了一个调试器.例如:
def show_locals():
# put something in here that shows local_1.
local_1 = 123
show_locals() # I want this to show local_1.
Run Code Online (Sandbox Code Playgroud)
我把什么放在体内show_locals?如果我必须修改调用语句,我可以进行的最小修改是什么?
注意:当show_locals它与调用者位于不同的模块时,这必须起作用.
是否有可能写出一个类型特征,比如说is_callable<T>一个对象是否已operator()定义?如果调用运算符的参数事先已知,则很容易,但在一般情况下则不行.当且仅当至少有一个重载调用运算符被定义时,我希望特征返回true.
这个问题是相关的,并且有一个很好的答案,但它不适用于所有类型(仅限于 - 可int转换类型).此外,std::is_function工作,但只适用于正确的C++函数,而不是函子.我正在寻找更通用的解决方案.
我一直在研究Ruby并发现它的关键字"直到"和"除非"非常有趣.所以我认为在C/C++中添加类似关键字的好方法是什么.这就是我想出的:
#define until(x) while(!(x))
#define unless(x) if(!(x))
Run Code Online (Sandbox Code Playgroud)
我正在寻找一些建议.谁能提出更好的选择?
这是我编写的一个程序示例,用于说明我打算做什么:
#include <stdio.h>
#include <stdlib.h>
#define until(x) while(!(x))
#define unless(x) if(!(x))
unsigned int factorial(unsigned int n) {
unsigned int fact=1, i;
until ( n==0 )
fact *= n--;
return fact;
}
int main(int argc, char*argv[]) {
unless (argc==2)
puts("Usage: fact <num>");
else {
int n = atoi(argv[1]);
if (n<0)
puts("please give +ve number");
else
printf("factorial(%u) = %u\n",n,factorial(n));
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果你能指出一些可以在C或C++中使用的类似技巧的参考文献,那将是很棒的.
是否可以检查该类型T是否std::array为任意类型和大小?
我可以检查一个特定的数组,例如:
is_same<T, std::array<int,5>>::value
Run Code Online (Sandbox Code Playgroud)
但我想检查一下T是否有任何实例化std::array.像下面的东西(当然,这不会编译):
is_same<T, std::array>::value
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这一点(可能没有使用is_same)?