首页 \ 问答 \ 加载时显示ruby文件的完整路径名(Show full path name of the ruby file when it get loaded)

加载时显示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
更新时间:2022-05-04 22:05

最满意答案

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脚本会产生:

42

Go 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 after export 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 ...
  • 更新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 ...
  • 这没有内置功能; 你必须自己做。 下面是一个适用于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进行解析,这使得它可 ...
  • 扩展方法不像.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.
  • 做一个./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.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)