加载时显示ruby文件的完整路径名(Show full path name of the ruby file when it get loaded)
在阅读源代码时,我总是想知道文件加载时的完整路径,在ruby中是否有回调方法来完成此操作,或者使用其他方式来执行此操作? 提前致谢。
编辑澄清从评论:
我想知道在执行这一行时加载的“somefile”所在的位置:“load somefile”
When reading source code, I always want to know the full path of the file when it is loaded, is there any callback method in ruby to accomplish this, or any other way to do this? thanks in advance.
EDIT Clarification from the comments:
I want to know where the loaded "somefile" is located while I execute this line: "load somefile"
原文:https://stackoverflow.com/questions/456379
最满意答案
Go 1.5增加了对构建可从C调用的共享库的支持(从而通过FFI从Ruby)。 这使得该过程比1.5版之前的版本(当需要编写C glue层时更容易)以及Go运行时现在可用,使其在现实生活中非常有用(以前不可能使用goroutine和内存分配,因为他们需要Go运行时,如果Go不是主要入口点,那么这是不可用的)。
goFuncs.go:
package main import "C" //export GoAdd func GoAdd(a, b C.int) C.int { return a + b } func main() {} // Required but ignored
请注意,
//export GoAdd
注释是每个导出的函数所必需的;export
后的符号是函数如何导出。goFromRuby.rb:
require 'ffi' module GoFuncs extend FFI::Library ffi_lib './goFuncs.so' attach_function :GoAdd, [:int, :int], :int end puts GoFuncs.GoAdd(41, 1)
该图书馆建有:
go build -buildmode=c-shared -o goFuncs.so goFuncs.go
运行Ruby脚本会产生:
42Go 1.5 added support for building shared libraries that are callable from C (and thus from Ruby via FFI). This makes the process easier than in pre-1.5 releases (when it was necessary to write the C glue layer), and the Go runtime is now usable, making this actually useful in real life (goroutines and memory allocations were not possible before, as they require the Go runtime, which was not useable if Go was not the main entry point).
goFuncs.go:
package main import "C" //export GoAdd func GoAdd(a, b C.int) C.int { return a + b } func main() {} // Required but ignored
Note that the
//export GoAdd
comment is required for each exported function; the symbol afterexport
is how the function will be exported.goFromRuby.rb:
require 'ffi' module GoFuncs extend FFI::Library ffi_lib './goFuncs.so' attach_function :GoAdd, [:int, :int], :int end puts GoFuncs.GoAdd(41, 1)
The library is built with:
go build -buildmode=c-shared -o goFuncs.so goFuncs.go
Running the Ruby script produces:
42
相关问答
更多-
Go 1.5增加了对构建可从C调用的共享库的支持(从而通过FFI从Ruby)。 这使得该过程比1.5版之前的版本(当需要编写C glue层时更容易)以及Go运行时现在可用,使其在现实生活中非常有用(以前不可能使用goroutine和内存分配,因为他们需要Go运行时,如果Go不是主要入口点,那么这是不可用的)。 goFuncs.go: package main import "C" //export GoAdd func GoAdd(a, b C.int) C.int { return a + b ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
更新2015年 :可能从Go 1.5 https://blog.filippo.io/building-python-modules-with-go-1-5/ 使用Go 1.5,您可以构建.so对象并将其作为Python模块导入,直接从Python运行Go代码(而不是C)。 Update 2015: possible as of Go 1.5 https://blog.filippo.io/building-python-modules-with-go-1-5/ with Go 1.5 you can bu ...
-
根据Golang中hashmap的值获取密钥的简单方法(Simple way of getting key depending on value from hashmap in Golang)[2021-10-31]
这没有内置功能; 你必须自己做。 下面是一个适用于map[string]int的示例函数,您可以为其他地图类型进行调整: func mapkey(m map[string]int, value int) (key string, ok bool) { for k, v := range m { if v == value { key = k ok = true return } } return } 用法: key, ok := mapke ... -
这样做的主要原因是速度和现有功能的重复使用。 首先,速度。 C通常比Ruby快得多,因为您可以避免Ruby VM并且可以进行手动内存管理。 在应用程序的性能关键部分(例如数据库驱动程序)中,这可能意味着整个应用程序运行时间的显着改进,主要是因为您没有生成大量的Ruby对象来包装基元,而不必调用垃圾收集器自己清理。 其次,通过编写C扩展,您可以与C库中已存在的代码进行交互。 Linux生态系统与功能强大,经过良好测试的C库齐平,可提供许多常用功能。 例如, Nokogiri使用libxml进行解析,这使得它可 ...
-
golang中的扩展方法?(extension methods in golang?)[2024-05-21]
扩展方法不像.NET那样受支持。 您可以做的最接近的事情是从字符串创建一个类型并在该类型上创建一个方法: type MyString string func (m *MyString) Method() { } func main() { var s MyString = "" s.Method() } Extension methods are not supported the way it is in .NET. The closest thing you can do is cr ... -
解决方案是添加: new(bf) BitField(); 到BitFieldNew(大小); 初始化struct和boost :: dynamic_bitset。 The solution was to add: new(bf) BitField(); to BitFieldNew(size); to initialize the struct and boost::dynamic_bitset.
-
禁用ruby扩展(Disable ruby extension)[2022-11-03]
做一个./configure --help并注意选项 --with-exts=EXTS --with-out-ext=EXTS 我有相同的设置,Win 7 x86_64与cygwin 1.7,并没有建立该扩展的问题。 gcc命令行与您的相同。 我想,Microsoft文档提到了你可以检查的mlang.dll。 Do a ./configure --help and note the options --with-exts=EXTS --with-out-ext=EXTS I've got the sam ... -
虽然这是一个微不足道的例子,猴子补丁通常很好。 最好在类的命名空间中使用该方法,而不是在不知名的地方浮动。 请记住,如果你正在写一个宝石或其他人打算使用的东西,你的猴子补丁有可能干扰他们的猴子补丁。 对于像这样的简单情况,最好避免它只是为了安全。 While this is a somewhat trivial example, monkey patches are generally fine. Better to have the method in the class's namespace than ...
-
您的版本已经是最简单,最短和惯用的方式。 Your version is already the simplest, shortest and idiomatic way to do it.