我在Ruby中有一个字符串,s(说)这可能拥有的任何标准行结尾的(\n,\r\n,\r).我想将所有这些转换为\ns.什么是最好的方式?
这似乎是一个超常见的问题,但没有太多关于它的文档.显然有简单的原油解决方案,但有没有内置的处理这个?
优雅,惯用的Ruby解决方案是最好的.
编辑:意识到^M并且\r是一样的.但仍有三种情况.(见维基百科.)
我试图用这种方式制作一个简单的Vector类(数学):
template <int D, typename T = float>
class Vector
{
T m[D];
// ...
};
Run Code Online (Sandbox Code Playgroud)
D维数在哪里.如果是2,则向量将存储两个类型的值T.
如何声明构造函数以获取D类型的参数T?
Vector<2> v(1.0f, -6.3f);
Run Code Online (Sandbox Code Playgroud)
D如果特定号码,如何添加功能?我希望添加GetX()if D> = 1,GetY()如果D> = 2且GetZ()if D> = 3,但以下代码应生成编译时错误:
Vector<2> v(1.0f, -6.3f);
cout << v.GetZ() << endl;
Run Code Online (Sandbox Code Playgroud)
如果D <1,如何生成编译时错误?
我没有遵循任何具体的标准,任何事都适合我.
我想写一个函数,用它的参数调用另一个函数.看看我希望它如何工作:
int sum(int a, int b) { return a + b }
int succ(int a) { return a + 1 }
int size(char* str) { return strlen(str) }
int call(???) { ??? }
int main() {
cout << call(sum, 1, 2) << endl;
cout << call(succ, 41) << endl;
cout << call(size, "teste") << endl;
}
Run Code Online (Sandbox Code Playgroud)
预期产量:
3
42
5
Run Code Online (Sandbox Code Playgroud)
如何编写call函数(假设返回值始终相同)?我能想到的唯一方法就是这样:
template<typename T> int call(T func, int a, int b) { return func(a, b) }
template<typename T> int …Run Code Online (Sandbox Code Playgroud) 我想创建一个通用函数,它返回一个函数指针,指向C/C++中的另一个函数.但是,第二个返回的函数应该能够使用第一个函数的变量.
例,
typedef double (*func_t)(double);
func_t inverse(func_t fn) {
// define another function here that uses fn
double solve(double x) {
// use fn
}
return &solve;
}
double sqr(double x) { return x * x; }
int main() {
func_t inv = inverse(sqr);
printf("sqrt %d = %f\n", 100, inv(100));
}
Run Code Online (Sandbox Code Playgroud)
显然gcc,g ++不允许我这样做.我可以在不使用类或结构的情况下实现此目的.
我从ruby 网站(1.9.2-p180)下载了持续稳定的ruby源代码,并在Windows上用MinGW 4.5.2-TDM和MSYS编译.要编译我跑sh configure和make.我完全按照预期获得了msvcrt-ruby191.dll和libmsvcrt-ruby191.dll.a.然后我写了这段代码:
#include <ruby.h>
int main() {
ruby_init();
rb_funcall2(Qnil, rb_intern("p"), 1, (VALUE[]){INT2FIX(0)});
ruby_finalize();
}
Run Code Online (Sandbox Code Playgroud)
我编译g++,链接到ruby的dll.当我运行可执行文件时,我收到此错误消息:
<main>: [BUG] Segmentation fault
ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-mingw32]
-- control frame ----------
c:0002 p:---- s:0004 b:0004 l:000003 d:000003 CFUNC :p
c:0001 p:0000 s:0002 b:0002 l:00120c d:00120c TOP
---------------------------
-- Ruby level backtrace information ----------------------------------------
ruby:0:in `p'
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries. …Run Code Online (Sandbox Code Playgroud) 我现在正在使用此代码:
size_t argc(std::function<Foo()>)
{ return 0; }
size_t argc(std::function<Foo(Bar)>)
{ return 1; }
size_t argc(std::function<Foo(Bar, Bar)>)
{ return 2; }
size_t argc(std::function<Foo(Bar, Bar, Bar)>)
{ return 3; }
// ...
Run Code Online (Sandbox Code Playgroud)
但它有点丑陋且有限(用户不能argc使用具有任意数量参数的函数调用.)有更好的方法吗?
注意:返回类型和参数类型始终相同.我知道我可以使用模板来接受任何类型,但我不需要它.
如果我采用ruby代码
puts "Hello World!"
Run Code Online (Sandbox Code Playgroud)
并使用Ruby的C API重写它
#include "ruby.h"
int main() {
ruby_init();
rb_funcall(Qnil, rb_intern("puts"), 1, rb_str_new2("Hello World!"));
ruby_finalize();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并编译它,这是一种编译Ruby代码的方法吗?
如果我创建一个Ripper用于解析Ruby代码并将其重写为C的程序,我可以将其称为"Ruby编译器"吗?有一些ruby代码不能以这种方式在Ruby中重写吗?有人曾尝试过编写这种"编译器"吗?
我的代码:
#include <iostream>
using namespace std;
class Foo
{
public:
int bar;
Foo()
{
bar = 1;
cout << "Foo() called" << endl;
}
Foo(int b)
{
bar = 0;
Foo();
bar += b;
cout << "Foo(int) called" << endl;
}
};
int main()
{
Foo foo(5);
cout << "foo.bar is " << foo.bar << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Foo() called
Foo(int) called
foo.bar is 5
Run Code Online (Sandbox Code Playgroud)
为什么foo.bar价值不是6?Foo()被调用但未设置bar为1.为什么?
我在Ruby中使用元编程开玩笑,我做了这段代码:
class Class
def ===(other)
other.kind_of?(self)
end
end
class FakeClass
def initialize(object)
methods.each {|m| eval "undef #{m}" if m.to_sym != :methods }
define = proc do |m|
eval(<<-END)
def #{m}(*a, &b)
@object.#{m}(*a, &b)
rescue Object
raise $!.class, $!.message.gsub("FakeClass", @object.class.to_s),
$!.backtrace-[$!.backtrace[-caller.size-1]]
end
END
end
object.methods.each {|m| define[m] }
def method_missing(name, *a, &b)
if @object.methods.include?(name.to_s)
define[name]
eval "@object.#{name}(*a, &b)"
elsif @object.methods.include?("method_missing")
eval "@object.#{name}(*a, &b)"
else
super
end
rescue Object
raise $!.class, $!.message.gsub("FakeClass", @object.class.to_s),
$!.backtrace-[$!.backtrace[-caller.size-1]]
end
@object = object
end
end
Run Code Online (Sandbox Code Playgroud)
这会创建一个模仿对象的假类.看:
a …Run Code Online (Sandbox Code Playgroud) 如果我有以下结构:
struct Foo { int a; };
Run Code Online (Sandbox Code Playgroud)
代码是否符合C++标准?我的意思是,它不能产生"未定义的行为"吗?
Foo foo;
int ifoo;
foo = *reinterpret_cast<Foo*>(&ifoo);
void bar(int value);
bar(*reinterpret_cast<int*>(&foo));
auto fptr = static_cast<void(*)(...)>(&bar);
fptr(foo);
Run Code Online (Sandbox Code Playgroud) c++ ×6
ruby ×4
c++11 ×3
c ×2
templates ×2
class ×1
constructor ×1
interpreter ×1
line-breaks ×1
newline ×1