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
5
votes
2 answers

LINQ/IEnumerable Skip().Take() efficiency with used with "yield return"

I have a question about the efficiency of Skip() and Take() when used with IEnumerable<>. I am returning all my data lists with IEnumerable<> and i use 'yield return' to prevent me from having to allocate large amounts of memory to pass back the…
user3328317
  • 51
  • 1
  • 3
5
votes
4 answers

Generate and repeat number in C#

I want to generate an array that has 144 number from 1->36 in random order (so each number is repeated 4 times). Can we use Enumerable.Repeat and Enumerable.Range to do that. If yes than please explain to me how?
A New Chicken
  • 703
  • 1
  • 8
  • 18
5
votes
1 answer

Get indices for sorted permutation of an array in Ruby?

Let's say I have an Array ary = [0.0, 1.0, 5.0, 1.0, -2.0, 3.5], and I want as output another array of the same size containing ary's indices in sorted-by-value-order. In other words, the output should be [4,0,1,3,5,2]. Is there an efficient way to…
Translunar
  • 3,739
  • 33
  • 55
5
votes
1 answer

What's the best way to return an Enumerator::Lazy when your class doesn't define #each?

Enumerable#lazy relies on your enumerable providing an #each method. If your enumerable doesn't have an #each method you can't use #lazy. Now Kernel#enum_for and #to_enum provide the flexibility to specify an enumeration method other than…
Bill Burcham
  • 739
  • 5
  • 12
5
votes
4 answers

Ruby removing duplicates in enumerable lists

Is there a good way in ruby to remove duplicates in enumerable lists (i.e. reject, etc.)
Daniel
  • 16,026
  • 18
  • 65
  • 89
5
votes
4 answers

Ruby enumerables don't keep the same class

I have a class that represents a collection. I included the Enumerable module into it and defined the method #each, so that I get all its methods. But the problem is that Enumerable's methods don't keep the same class. So, for example, if my class…
Janko
  • 8,985
  • 7
  • 34
  • 51
5
votes
2 answers

Is there an idiomatic way to operate on 2 arrays in Ruby?

a = [3, 4, 7, 8, 3] b = [5, 3, 6, 8, 3] Assuming arrays of same length, is there a way to use each or some other idiomatic way to get a result from each element of both arrays? Without using a counter? For example, to get the product of each…
B Seven
  • 44,484
  • 66
  • 240
  • 385
4
votes
2 answers

Sort Objects by Boolean values in Ruby

My apologies if this has been answered before or is obvious...did some searching here and on the Goog and couldn't find an answer. I'm looking to sort an array of Providers by price and whether they are a preferred_provider? (true or false) For…
Kevin Dewalt
  • 757
  • 9
  • 24
4
votes
1 answer

Requirements for including Enumerable

I've been trying to design my read only Array data structure and I really like the idea of providing traversing options with Enumerable class, but I cannot find requirements of it. In some examples that I've looked, the C implementation looks for…
farnoy
  • 7,356
  • 2
  • 20
  • 30
4
votes
1 answer

Extend Enumerable.Range

Possible Duplicate: Enumerable.Range implementation I ran into an issue where i need to add a very long value (Int64) into a list, The value is 600851475143, i want to create a List that contains all the int upto this value, but…
manav inder
  • 3,531
  • 16
  • 45
  • 62
4
votes
2 answers

JavaScript: How to Define a Non-Enumerable Method without using Object.defineProperty?

I want to add a method to Object, but now all arrays and object have it. When I use for(.. in ..), it is enumarated and this is a problem for my software. So, I need to make my method non-enumerable. I know there is a Object.defineProperty(), but it…
Federico Razzoli
  • 4,901
  • 1
  • 19
  • 21
4
votes
1 answer

Enumerable::each_with_index now optionally takes a arguments in Ruby 1.9. What significance and/or what is a use case for that?

In Ruby 1.8.7 and prior, Enumerable::each_with_index did not accept any arguments. In Ruby 1.9, it will accept an arbitrary number of arguments. Documentation/code shows that it simply passes those arguments along to ::each. With the built in and…
Ryan
  • 2,073
  • 1
  • 19
  • 33