我是PHP开发人员,目前我正在学习Rails(3),当然还有Ruby.我不想相信魔法,所以我尽可能地了解Rails背后发生的事情.我发现有趣的是类似于has_one或belongs_to在ActiveRecord模型中的方法调用.
我试图重现这一点,并带来了天真的例子:
# has_one_test_1.rb
module Foo
class Base
def self.has_one
puts 'Will it work?'
end
end
end
class Model2 < Foo::Base
has_one
end
Run Code Online (Sandbox Code Playgroud)
只是运行这个文件将输出"它会工作吗?",正如我所料.
在搜索rails源代码时,我找到了负责的函数:def has_one(association_id,options = {}).
这怎么可能,因为它显然是一个实例(?)而不是一个类方法,它应该不起作用.
经过一番研究,我找到了一个可以回答的例子:
# has_one_test_2.rb
module Foo
module Bar
module Baz
def has_one stuff
puts "I CAN HAS #{stuff}?"
end
end
def self.included mod
mod.extend(Baz)
end
end
class Base
include Bar
end
end
class Model < Foo::Base
has_one 'CHEEZBURGER'
end
Run Code Online (Sandbox Code Playgroud)
现在运行has_one_test_2.rb文件将输出I CAN HAS …
我有一个有两列的表:
+---------+--------+
| keyword | color |
+---------+--------+
| foo | red |
| bar | yellow |
| fobar | red |
| baz | blue |
| bazbaz | green |
+---------+--------+
Run Code Online (Sandbox Code Playgroud)
我需要在PostgreSQL中做一些单热编码和转换表来:
+---------+-----+--------+-------+------+
| keyword | red | yellow | green | blue |
+---------+-----+--------+-------+------+
| foo | 1 | 0 | 0 | 0 |
| bar | 0 | 1 | 0 | 0 |
| fobar | 1 | 0 | 0 | 0 …Run Code Online (Sandbox Code Playgroud) 编译此程序时遇到问题:
use std::env;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn main() {
let args: Vec<_> = env::args().skip(1).collect();
let (tx, rx) = mpsc::channel();
for arg in &args {
let t = tx.clone();
thread::spawn(move || {
thread::sleep(Duration::from_millis(50));
let _new_arg = arg.to_string() + "foo";
t.send(arg);
});
}
for _ in &args {
println!("{}", rx.recv().unwrap());
}
}
Run Code Online (Sandbox Code Playgroud)
我从命令行读取所有参数,并模拟对线程中的每个参数做一些工作.然后我打印出这项工作的结果,我使用一个频道.
error[E0597]: `args` does not live long enough
--> src/main.rs:11:17
|
11 | for arg in &args {
| ^^^^ does not live long enough
...
24 …Run Code Online (Sandbox Code Playgroud) 在Python中,有dict.inspect()方法返回元组列表(链接).在ruby中是否有类似的方法来实现,一个数组的数组?
#input
{:a => 1, :b => 2}
#result
[[:a, 1], [:b, 2]]
Run Code Online (Sandbox Code Playgroud) 我有一个 Rust 库,它为FFI. 我假设我必须设置crate-type为cdylib- 因为我想从 Ruby 和 PHP 调用这些函数(通过ffiruby gem调用)。但是我无法将它从 OSX 交叉编译到 Linux。我试图遵循一些使用的教程musl libc- 这是针对静态库的,但我还没有找到其他任何东西。
所以链接器是这样定义的:
# .cargo/config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
Run Code Online (Sandbox Code Playgroud)
我试图用以下方法编译它:
cargo build --release --target x86_64-unknown-linux-musl
Run Code Online (Sandbox Code Playgroud)
但是有直接的错误:
error: cannot produce cdylib for `my-crate-name` as the target `x86_64-unknown-linux-musl` does not support these crate types
Run Code Online (Sandbox Code Playgroud)
我的问题是:什么目标/链接器对可用于交叉编译cdylib?为什么 musl 不支持这些 crate 类型?甚至有可能吗?