Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[] method of Ruby String

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.

like image 430
eric2323223 Avatar asked Dec 28 '08 17:12

eric2323223


2 Answers

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.

like image 43
Priit Avatar answered Sep 28 '22 04:09

Priit


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!

like image 105
J Cooper Avatar answered Sep 28 '22 02:09

J Cooper