Questions tagged [enumerable]

"enumerable" refers to an ordering scheme that enables items in a set, sequence or collection to be readily addressed or traversed.

Enumerable is a Ruby module which can be included in other Ruby classes in order to allow iteration via #map/#collect, #select, #each, and so on.

Enumerable also manifests as the class Enumerator (documentation), which is returned when an iteration method is called without a block (e.g., array.each instead of array.each { |x| puts x }).

Enumerable objects may also utilize chained iteration, e.g., array.each.with_indices instead of array.each_with_indices.

Ruby 2.0 also introduces the concept of lazy enumeration.

In .NET, Enumerable is the class that wraps most common LINQ extension methods.

603 questions
9
votes
2 answers

What is the difference between iterable and enumerable in JS? I am going through For/of and For/In loop and these terms are coming up frequently

I am coming across terms Iterable and Enumerable while studying For/in and For/of loops. Objects are supposed be enumerable and we have to use For/in loop to loop over the properties of the object and For/of to loop over the arrays and strings. I…
Mahwash
  • 91
  • 1
  • 4
9
votes
1 answer

Moq First() Last() and GetEnumerator() wierdness

I am Moqing my Route Parts from a rps = new List... (3 Route Parts) and Moqing GetEnumerator() for my Route as below route.Setup(ro => ro.GetEnumerator()).Returns(rps.GetEnumerator()); but the Moq fails in the following code with "Sequence…
Spud
  • 243
  • 1
  • 2
  • 12
9
votes
7 answers

Is there a way to specify an anonymous empty enumerable type?

I'm returning a Json'ed annonymous type: IList listOfStuff = GetListOfStuff(); return Json( new { stuff = listOfStuff } ); In certain cases, I know that listOfStuff will be empty. So I don't want the overhead of calling…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
9
votes
3 answers

Why does Enumerable#find/#detect return an Array even when called on an Hash?

The documentation for Enumerable#find/#detect says: find(ifnone = nil) { |obj| block } → obj or nil find(ifnone = nil) → an_enumerator Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls…
steveyang
  • 9,178
  • 8
  • 54
  • 80
9
votes
2 answers

How can I get a list from a Ruby enumerable?

I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby? I know of : elements = [] enumerable.each {|i| elements << i} I also know of the inject alternative. Is there some ready…
Geo
  • 93,257
  • 117
  • 344
  • 520
8
votes
3 answers

Is there a method in Ruby that does the opposite of find?

a, b, c = 0, 1, 2 [a, b, c].find(&:zero?) # => 0 Is there any method that finds the first element for which the block returns false? [a, b, c].the_method(&:zero?) # => 1 In other words, it would behave the same way as: [a, b,…
Pablo B.
  • 1,823
  • 1
  • 17
  • 27
8
votes
4 answers

Anonymous Type with Linq and Guid

I have a simple table: ID | Value When I do this: var sequence = from c in valuesVault.GetTable() select new {RandomIDX = Guid.NewGuid(), c.ID, c.Value}; each element in the projection has the value of the same guid... How do I…
dexter
  • 7,063
  • 9
  • 54
  • 71
8
votes
6 answers

Ruby longest word in array

I built this method to find the longest word in an array, but I'm wondering if there's a better way to have done it. I'm pretty new to Ruby, and just did this as an exercise for learning the inject method. It returns either the longest word in an…
clem
  • 3,524
  • 3
  • 25
  • 41
8
votes
5 answers

Possible to turn callback calls into IEnumerable

I'm writing a wrapper around a 3rd party library, and it has a method to scan the data it manages. The method takes a callback method that it calls for each item in the data that it finds. e.g. The method is essentially: void Scan(Action
Gareth
  • 2,424
  • 5
  • 26
  • 44
8
votes
4 answers

Equivalent of Ruby Enumerable.collect that returns an Enumerable?

In this code, I create an array of strings "1" to "10000": array_of_strings = (1..10000).collect {|i| String(i)} Does the Ruby Core API provide a way to get an enumerable object that lets me enumerate over the same list, generating the string…
mackenir
  • 10,801
  • 16
  • 68
  • 100
8
votes
6 answers

Ruby: Is there something like Enumerable#drop that returns an enumerator instead of an array?

I have some big fixed-width files and I need to drop the header line. Keeping track of an iterator doesn't seem very idiomatic. # This is what I do now. File.open(filename).each_line.with_index do |line, idx| if idx > 0 ... end end # This…
Samuel Danielson
  • 5,231
  • 3
  • 35
  • 37
8
votes
4 answers

C# IEnumerable, IEnumerator Reset Function Not Get Called

I'm basicly trying to make my class able to iterate using foreach. I read this tutorial. MSDN. It seems very straight forward. However, I have a problem when I want to iterate second time. I debugged it; and it turned out that it doesn't call the…
Sait
  • 19,045
  • 18
  • 72
  • 99
7
votes
5 answers

how to compare to previous item in `each` iterator?

update: sorry, I fixed my program: a = [ 'str1' , 'str2', 'str2', 'str3' ] name = '' a.each_with_index do |x, i | if x == name puts "#{x} found duplicate." else puts x name = x if i!= 0 end end output: str1 str2 str2…
hey mike
  • 2,431
  • 6
  • 24
  • 29
7
votes
5 answers

Linq statement for an infinite sequence of successive halves

Given a starting number, imagine an infinite sequence of its successive halves. 1, 0.5, 0.25, 0.125, ... (Ignore any numerical instabilities inherent in double.) Can this be done in a single expression without writing any custom extension methods…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
7
votes
1 answer

What is the enumerable argument for in Object.create?

In what usages of Object.create do you want to set enumerable to true?
ryanve
  • 50,076
  • 30
  • 102
  • 137