[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

immutable and enumerable?

John Maclean

2/12/2006 1:13:00 PM

Every language and profession has it's own language. Ruby is no
exception. Can someone explain to me what the two terms above mean and
why they are so crucial to Ruby?

Are they methods/classes ( don't think so) /modules/some other thing?



--
John Maclean
MSc (DIC)
07739 171 531



2 Answers

Dave Burt

2/12/2006 2:43:00 PM

0

John Maclean asked:
> Every language and profession has it's own language. Ruby is no
> exception. Can someone explain to me what the two terms above mean and
> why they are so crucial to Ruby?
>
> Are they methods/classes ( don't think so) /modules/some other thing?

Immutable is not a Ruby-specific term. Its meaning in this group is pretty
much its dictionary definition, that is, "not subject to change." We (and
other OO programmers) apply it to objects that don't have "mutator" methods,
methods that change the object itself.

Say you have an object, and offer it to a function of some kind, then check
its value afterwards. If the object is immutable (like a Fixnum in Ruby)
your variable is guaranteed to be the same as before. Otherwise, that
function may have altered the object.

The method Object#freeze lets you make any object immutable.

Enumerable (in Ruby) is a module, included by Array and Hash. For
documentation:
http://www.ruby-doc.org/core/classes/Enume...

From that page:
" The Enumerable mixin provides collection classes with several traversal
and searching methods, and with the ability to sort. The class must provide
a method each, which yields successive members of the collection. If
Enumerable#max, min, or sort is used, the objects in the collection must
also implement a meaningful <=> operator, as these methods rely on an
ordering between members of the collection."

Cheers,
Dave


Robert Klemme

2/12/2006 3:08:00 PM

0

Dave Burt <dave@burt.id.au> wrote:

> Enumerable (in Ruby) is a module, included by Array and Hash. For
> documentation:
> http://www.ruby-doc.org/core/classes/Enume...
>
> From that page:
> " The Enumerable mixin provides collection classes with several
> traversal and searching methods, and with the ability to sort. The
> class must provide a method each, which yields successive members of
> the collection. If Enumerable#max, min, or sort is used, the objects
> in the collection must also implement a meaningful <=> operator, as
> these methods rely on an ordering between members of the collection."

The term "enumerable" is also often used to denote classes that include this
module and implement an each method as in "String is enumerable."

Kind regards

robert