在模块中打开类
我知道我可以执行以下命令向 String 类添加方法
class String
def do_something
puts self.size
end
end
var = "test"
var.do_something
,这将返回 4
我希望能够拥有一个带有接受 String 的函数的模块,但能够调用该字符串上的 do_something
方法(例如见下文)-可能吗?
编辑:添加了不起作用的示例代码
module TestModule
class String
def do_something
puts self.size
end
end
def self.test(str)
str.do_something
end
end
这给出了错误:undefined method 'do_something' for "hello":String (NoMethodError)
I know I can execute the following to add methods to the String class
class String
def do_something
puts self.size
end
end
var = "test"
var.do_something
and this will return 4
I want to be able to have a module with a function that takes in a String, but be able to call the do_something
method on this string (see below for example) - is it possible?
EDIT: Added sample code that is not working
module TestModule
class String
def do_something
puts self.size
end
end
def self.test(str)
str.do_something
end
end
This gives the error: undefined method 'do_something' for "hello":String (NoMethodError)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照编写代码的方式,您定义了一个名为 TestModule::String 的新类。如果要修改内置 Ruby String 类,并且要将声明保留在模块内,则需要使用 String 的完全限定名称(带有“::”)。
添加“::”告诉 Ruby 您想要的 String 类不是 TestModule 的一部分。
在同一文件中的 TestModule 之外声明 String 可能会更干净。
如果您不想污染全局 String 类,那么 您想要添加方法的字符串实例。
The way your code is written, you're defining a new class called TestModule::String. If you want to modify the built-in Ruby String class, you need to use the fully-qualified name of String (with the ""::") if you want to keep the declaration inside the module.
Adding the "::" tells Ruby that the String class that you want is not part of the TestModule.
It's probably cleaner to just declare String outside of TestModule in the same file.
If you don't want to pollute the global String class, you could just modify the specific String instance that you want to add the method to.
也许是这个?
编辑由于问题的更改而更改
只需将
doSomething
的定义放在TestModule
类之外。Maybe this?
Edit Changed due to the change in the question
Just put the definition of
doSomething
outside out theTestModule
class.