作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用一个始终使用字符串作为 ID 的库。我想制作一个可以代替这些 id 的类,但看起来像现有代码的字符串。例如。我有一个看起来像这样的现有测试:
require 'test/unit'
class IdString < Hash
def initialize(id)
@id = id
end
def to_s
@id
end
end
class TestGet < Test::Unit::TestCase
def test_that_id_is_1234
id = IdString.new('1234')
assert_match(/1234/, id)
end
end
不幸的是,这失败了:
TypeError: 无法将 IdString 转换为 String
有没有办法在不更改所有期望 id 为字符串的现有代码的情况下解决此问题?
最佳答案
您应该实现to_str
方法,该方法用于隐式转换:
def to_str
@id
end
关于Ruby:想要将对象用作字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17084372/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!