当我阅读 Beast 的源代码时,我发现很多这样的代码:
<%= 'Password'[:password_title] %>
对我来说,这似乎是对 [] 方法的调用,其中 Symbol 作为 String 的输入参数,但我在 ruby API 中没有找到 String [] 方法的这种类型的参数。这是什么意思?提前致谢。
当我阅读 Beast 的源代码时,我发现很多这样的代码:
<%= 'Password'[:password_title] %>
对我来说,这似乎是对 [] 方法的调用,其中 Symbol 作为 String 的输入参数,但我在 ruby API 中没有找到 String [] 方法的这种类型的参数。这是什么意思?提前致谢。
这是Beast使用的“Gibberish”插件添加的一种方法,用于国际化。请记住,Ruby 中的类是开放的,因此在这种情况下您不能总是依赖标准 API!
在野兽源代码中,查看正在修改 String 类以接受括号函数中的符号的乱码插件。
String 类本身并没有通过应用str[symbol]
方法做任何合理的事情。
str[fixnum] => fixnum or nil
str[fixnum, fixnum] => new_str or nil
str[range] => new_str or nil
str[regexp] => new_str or nil
str[regexp, fixnum] => new_str or nil
str[other_str] => new_str or nil
这些是我发现的。如果这里的符号等于String,我还是没看懂代码的意思。为什么不简单地使用:
<%= 'password' %>
甚至:
password