ruby - caller_method 返回的不是我期望的值
问题描述
我想知道,什么方法调用另一种方法(我只是想创建简单的 expect("string").to eq("string") 模型(就像在 RSpect 中一样,但更容易)。
但我得到“主要”,那是什么?(我第一次看到“主要”)
public
def expect(message)
message.to_s
end
def to
caller_method = caller_locations.first.label
puts caller_method
end
expect("test").to #=> <main>
#what output i expected:
expect("test").to #=> expect
我的目标:
#first i need to do something like that:
expect("test").to eq("test") #=> true
#final must look like this:
expect(expect("test").to eq("test")).to eq(true) #=> true
解决方案
我建议不要caller_method
在这种情况下使用。相反,创建一个其方法返回的类self
- 这样它们就可以链接:
module Expectation
attr_accessor :caller_method
def expect(arg)
self.caller_method = "expect"
self
end
def to
caller_method
end
end
include Expectation
expect("foo").to
# => "expect"
显然这只是一个起点,实际上还没有进行任何比较/验证。但希望你能理解这种模式。关键是返回self
以创建可链接的 API,并使用类似的东西存储内部状态attr_accessor
推荐阅读
- android - 在 Android 上使用 DocuSign SDK 缓存模板需要多少可用空间?
- macos - terraform 手动安装插件失败
- regex - Combine two references into one or some serious regex work
- go - go get fails for hashicorp/levant
- python - Get the first layer of html sections only using Beautiful Soup using recursive strategy
- python - Sorting List of Dataframes
- google-chrome-extension - Chrome 扩展程序无法识别身份
- wordpress - 如何制作一个选择字段,以便在选择一个选项时,总价格会发生变化?电子商务
- php - 比较 php MySQL 查询结果数组以从其中一个数组中获取关联值
- python - 无需单击按钮即可下载文件的脚本