[英]How can I run a private method in a module in rails console?
I have an error in a private method in a helper file. 我在帮助文件中的私有方法中出错。 The helper file looks something like below with module
HxHelper
. 带有模块
HxHelper
的帮助文件如下所示。 I want to run method_2
in rails console to recreate the error in my local system. 我想在Rails控制台中运行
method_2
以在本地系统中重新创建错误。
module HxHelper
def method_1{
"key_1": "@h.htype"
"key_2": "value_2"
+ method_2}
end
private
def method_2{
"key1": "value_1"}
end
In my controller file I define a new class and include the previous helper file. 在我的控制器文件中,我定义了一个新类并包含以前的帮助器文件。 I have tried the following.
我尝试了以下方法。
obj = Class.new
and obj.method_1
. obj = Class.new
和obj.method_1
。 I get an error undefined method type in method_1. HxHeleper::method_1
: Error - method_1 is not defined in HxHelper module.
HxHeleper::method_1
: Error - method_1 is not defined in HxHelper module.
method_2
as self
: Doesn't work. method_2
定义为self
:不起作用。 Can someone help me understand what I am doing wrong? 有人可以帮助我了解我在做什么错吗?
You have some syntax errors in this example. 在此示例中,您有一些语法错误。 You want to use commas at the end of the lines of your hash, you can't add two hashes together, you instead need to merge.
您要在哈希行的末尾使用逗号,不能将两个哈希加在一起,而需要合并。 Merging will take the receiver (the thing you're calling merge on) and override any values from the argument.
合并将接收器(您正在调用合并的对象)并覆盖参数中的所有值。 Additionally, when using
:
in your hash, your keys end up being symbols, which means you don't need the quotes. 此外,在哈希中使用
:
时,您的键最终会变成符号,这意味着您不需要引号。
This would be the proper way to define the helper module. 这将是定义助手模块的正确方法。
module HxHelper
def method_1
{
key_1: "@h.htype",
key_2: "value_2",
}.merge(method_2)
end
private
def method_2
{
key1: "value_1",
}
end
end
Then you can use it like this: 然后,您可以像这样使用它:
class Test
include HxHelper
end
t = Test.new
t.method_1
This will return: 这将返回:
{:key_1=>"@h.htype", :key_2=>"value_2", :key1=>"value_1"}
If you call t.method_2
, you get an error about calling a private method. 如果调用
t.method_2
,则会出现有关调用私有方法的错误。
If method_2's hash had a key of key_1
instead, your return value would be: 如果method_2的哈希值具有键
key_1
,则您的返回值为:
{:key_1=>"value_1", :key_2=>"value_2"}
because the :key_1
from the argument overrode the one on the receiver Hash. 因为参数中的
:key_1
覆盖接收者哈希中的那个。
If you wanted to call that private method, you could do: 如果要调用该私有方法,则可以执行以下操作:
t.send(:method_2)
If you had a method that took arguments, you just add them after the symbol of the method name: 如果您有一个采用参数的方法,则只需在方法名称的符号后添加它们:
private
def test(num1, num2)
return(num1 + num2)
end
send(:test, 1, 2)
This would return 3 这将返回3
Assuming we can get around your syntax issues, the standard answer to 'how do I call a private method in Ruby?' 假设我们可以解决您的语法问题,那么“我如何在Ruby中调用私有方法?”的标准答案是? is with .send(), as in
obj.send(:private_method)
. 与obj.send(
obj.send(:private_method)
中的obj.send(:private_method)
。
After you manually debug this, learn to write automated tests. 手动调试后,学习编写自动化测试。 Rails has exemplary, industry-leading support for them.
Rails为他们提供了业界领先的典范支持。 The tests will take over the role of experimentation and investigation that you are currently abusing the console for.
测试将取代您目前正在滥用控制台的实验和调查角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.