immutable and enumerable?

J

John Maclean

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?
 
D

Dave Burt

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/Enumerable.html

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
 
R

Robert Klemme

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top