Can anyone explain what "Enumeration" is? What it does and when a construct called enumerable? A simple example may help me to understand it better. I want to know the core of it.
on 2013-02-01 21:13
on 2013-02-01 21:36
Hello, an enumeration is a general term used to describe (in computer science at last) a list of elements of a collectioen. An Enumerable is a specific Ruby-core module used to a) mark a class' instances as collections of items which can be iterated through (though usually you won't ask ary.is_a? Enumerable but rather check if the object you're handling responds to the methods you need) and/or b) to extend the collection class by a huge amount of functionality (e.g. map, find, include? - the full list can be found in the documentation of Enumerable). Kind regards, Calvin
on 2013-02-01 21:43
Calvin B. wrote in post #1094810: > Hello, > > an enumeration is a general term used to describe (in computer science > at last) a list of elements of a collectioen. An Enumerable is a > specific Ruby-core module used to We have an "array" as list,then what the special features Enm has over array?
on 2013-02-01 21:53
Am 01.02.2013 21:43, schrieb Arup Rakshit: > Calvin B. wrote in post #1094810: >> Hello, >> >> an enumeration is a general term used to describe (in computer science >> at last) a list of elements of a collectioen. An Enumerable is a >> specific Ruby-core module used to > > We have an "array" as list,then what the special features Enm has over > array? > Enumerable is a module that you can include into your own classes, making them searchable, sortable, ... (refer to the docs). Useful when you define a custom collection class (e.g. a LinkList, a UserList or whatever).
on 2013-02-01 22:03
Hi, sorry if I confused you, I just repeated the definition (as I remembered it) without really explaining it. In the case of an array, the array is the enumeration AND the collection. There are some cases in which the two differ. An example which hopefully clears that up: The collection of all integers. Since the collection is infinite, we can't really have a list of all integers (at least as data). We could, in this specific case, describe our collection in a way so we can list an arbitrary number of elements, but never all. Regards, Calvin
on 2013-02-02 06:07
Calvin B. wrote in post #1094821: > Hi, > > sorry if I confused you, I just repeated the definition (as I remembered > it) without really explaining it. In the case of an array, the array is > the enumeration AND the collection. There are some cases in which the > two differ. An example which hopefully clears that up: The collection of > all integers. Since the collection is infinite, we can't really have a > list of all integers (at least as data). We could, in this specific > case, describe our collection in a way so we can list an arbitrary > number of elements, but never all. > > Regards, > Calvin @calvin - Your example is good. But in this context with this example how array is differing from enumerator? Thanks
on 2013-02-02 06:58
On Fri, Feb 1, 2013 at 11:07 PM, Arup Rakshit <lists@ruby-forum.com> wrote: >> number of elements, but never all. >> >> Regards, >> Calvin > > @calvin - Your example is good. But in this context with this example > how array is differing from enumerator? An Array is a class which includes the properties of Enumerable: 1.9.3-head :003 > Array.ancestors => [Array, Enumerable, Object, Kernel, BasicObject] If the class Array was implemented in pure Ruby (which it's not), it would look something like this: class Array include Enumerable # class definition and overrides for Enumerable methods as needed3 end The Enumerable class provides methods for many of the actions you normally take on an Array, such as :each, :sort, :count, :first, and so on. Other classes also include Enumerable; Hash, File, Dir, Range, etc.
on 2013-02-02 13:02
Hello,
an enumerator (I hope you didn't mix enumerator and enumeration up; if
you did, I may just have confused you more.. Sorry if that's the case),
or iterator, is an object to traverse a collection. In ruby, you usually
get an enumerator by calling one of enumerable's methods without a
block. This enumerator itself provides all methods of enumerable, as
well as some other nice functionality. It's just another way to go
through a collection.
An example (taken from the documentation of enumerator[1]):
%w[foo bar baz].map.with_index { |w, i| "#{i}: #{w}" }
# => ["0: foo", "1: bar", "2: baz"]
Since there is no map_with_index function in Enumerable (or Array), we
can't write that example as concise without an enumerator. We could,
however, do this by hand:
a = []
%w[foo bar baz].each_with_index { |w, i| a << "#{i}: #{w}" }
# a is now ["0: foo", "1: bar", "2: baz"]
To wrap all that up:
-Collection: A collection of things, can be an array, a hash, a linked
list, a mathematical set, ... (this is a general concept implemented at
various places in ruby)
-Enumeration: A list of all elements in a collection. Sometimes a
collection itself is an enumeration, sometimes not (this is also a
general concept)
-Enumerable: A module providing a huge functionality for traversing and
manipulating collections (this is a ruby-specific thing)
-Enumerator / Iterator: An object to traverse a collection (this is a
general concept and implemented in ruby)
I hope I could help. :)
Regards,
Calvin
On 02.02.2013 06:07, Arup Rakshit wrote:
> @calvin - Your example is good. But in this context with this example
> how array is differing from enumerator? Thanks
[1]: http://www.ruby-doc.org/core-1.9.3/Enumerator.html
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.