Ruby 1.9.3中的Digest::MD5

8
我在这里遇到了一些奇怪的问题。 我有一个“验证器”,它依赖于ND5来哈希我们匹配为密码的某个字符串。当我运行测试时,问题出现了:
NoMethodError: undefined method `md5' for #<CASServer::Authenticators::Billing:0x007fd8e6c906a0>
./models/authenticators/billing.rb:63:in `validate'
./routes/login.rb:166:in `block (2 levels) in <class:Server>'
./routes/login.rb:158:in `each'
./routes/login.rb:158:in `block in <class:Server>'
(eval):2:in `click_button'
./features/step_definitions/when_steps.rb:32:in `/^I enter "(.*)" as username and the generated username password and log in$/'
./features/rubycas.login.feature:14:in `When I enter "username" as username and the generated username password and log in'

基本上,他不认为MD5是摘要库的一部分。这个问题发生在IDE中运行测试以及IRB控制台中:

1.9.3-p125 :001 > require "digest/md5" and Digest::MD5("test")
NoMethodError: undefined method `MD5' for Digest:Module

然而,当我运行以下命令时:

[root@DCUDEV01 /home/morn/rubycas/current]# ruby
require "digest/md5" and Digest::MD5("test")

我没有收到任何错误、转储或异常。Ruby只是接受它。 为了使这个MD5的东西起作用,我缺少什么?
2个回答

34

Digest::MD5不是一个方法,而是一个模块。请尝试:

Digest::MD5.digest("test")

1
请问您能提供该方法的文档链接吗? - Kostas
这里有文档:http://www.ruby-doc.org/stdlib-2.0/libdoc/digest/rdoc/。但是我无法找到Digest::MD5的官方文档。 - Pablo Marambio

0

我觉得这很令人困惑,似乎.digest不正确,但我也不能说它是错的...

评论者@reconbot在我回来改进这个QA之后才看到他的评论,我认为评论并不够显眼。

http://ruby-doc.org/stdlib-2.4.0/libdoc/digest/rdoc/Digest/MD5.html

以下是使用 md5 哈希为 Gibbon 使用 Mailchimp gem 的示例。
md5_hashed_email_address = Digest::MD5.hexdigest("john.doe@example.com")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接