views:

297

answers:

3

When I reading source code of Beast, I found a lot of code like this:

<%= 'Password'[:password_title] %>

It seems like a call to [] method with Symbol as input parameter to a String to me, but I didn't find such type of parameter of String [] method in the ruby API. What is this means? thanks in advance.

+1  A: 
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

These are what I found. If the symbol here is equals to String, I still don't understand the meaning of the code. Why not simply use:

<%= 'password' %>

or even:

password
eric2323223
Yes, it's a bit strange. I'll delete my answer. I didn't think before I offered it.
PEZ
Thanks for your edit, John :)
eric2323223
+4  A: 

In beast source, check out the gibberish plugin where String class is being modified to accept symbols in brackets function.

String class by itself does not do anything reasonable by applying str[symbol] method.

Priit
+11  A: 

It's a method added by the "Gibberish" plug-in Beast uses, for internationalization. Remember, classes in Ruby are open, so you can't always count on the standard API in cases like this!

J Cooper