On Monday 18 August 2008 05:10:58 Mayuresh K. wrote:
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
at least a few places you might otherwise use an Enum or a global
For example, suppose you have a function which can do three slightly
things. Say, for the sake of argument, it generates an input field,
be a checkbox, a dropdown, or a text field. You could call it like this:
And the function might look like this:
do some stuff common to all types
if type == :checkbox
# do something special for the checkbox
more common stuff…
… you get the idea.
And yes, they’re used widely as hash keys – in particular, to make up
fact that Ruby doesn’t have named arguments.
But if you want to know how that works, just look at any Rails example
has_many :accounts, :through => :customers
It’s that last bit that’s interesting, because you could always decide
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.