David Masover
8/20/2008 5:00:00 AM
On Monday 18 August 2008 05:10:58 Mayuresh Kathe wrote:
> Hello,
> What is the meaning of immutable, interned strings?
> Would like to know in common, general English :-)
Others have explained, mechanically. In terms of usage, you would use this in
at least a few places you might otherwise use an Enum or a global constant in
other languages.
For example, suppose you have a function which can do three slightly different
things. Say, for the sake of argument, it generates an input field, which can
be a checkbox, a dropdown, or a text field. You could call it like this:
field :checkbox
field :dropdown
field :text
And the function might look like this:
def field(type)
# do some stuff common to all types
if type == :checkbox
# do something special for the checkbox
end
# more common stuff...
... you get the idea.
And yes, they're used widely as hash keys -- in particular, to make up for the
fact that Ruby doesn't have named arguments.
But if you want to know how that works, just look at any Rails example code:
has_many :accounts, :through => :customers
It's that last bit that's interesting, because you could always decide that
you don't want customers. You want zebras:
has_many :zebras, :class => 'Customer'
has_many :stripes, :through => :zebras, :class => 'Account'
I don't know if that will actually work, but you get the idea.