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
6
votes
1 answer

Group array of hashes by multiple keys

I would like to group_by multiple keys: orders, idx, account, etc. The code below is a modified version of Ruby on Rails - Hash of Arrays, group by and sum by column name. Can anyone recommend a way to group on multiple keys and sum multiple…
user2012677
  • 5,465
  • 6
  • 51
  • 113
6
votes
2 answers

understanding comparable mixin and enumerable mixin

I am a newbie and learning ruby. Would like to have a better understanding of the question asked. I don't understand the use of comparable mixin and enumerable mixin. I mean we don't include these in our class when we need to use them, right? if we…
Akash Soti
  • 583
  • 2
  • 7
  • 13
6
votes
4 answers

How can I make DataTable enumerable?

I cannot use AsEnumerable() on DataTable, I'm using C# 3 but I'm just targeting 2.0 framework (LINQ capability is courtesy of LINQBridge). Is there any way I can make DataTable enumerable without using Select() ? bool isExisting =…
Hao
  • 8,047
  • 18
  • 63
  • 92
5
votes
3 answers

Help understanding yield and enumerators in Ruby

I would appreciate it if someone could help me understand the difference between using a Yielder in an Enumerator vs. just invoking yield in an Enumerator. The "Well-grounded Rubyist" suggests that one doesn't "yield from the block" but doesn't…
David
  • 5,991
  • 5
  • 33
  • 39
5
votes
1 answer

Ruby: Yield within enumerable

I'd like to be able to yield within an enumerable block, in order to create some boilerplate benchmarking code. Basically I'd like to do something this (simplified): def iterator( enumerable, &block ) iterations = enumerable.size counter = 0 …
Andrew
  • 42,517
  • 51
  • 181
  • 281
5
votes
1 answer

How does Enumerable#sum avoid floating point rounding errors?

I was playing around with some toy examples of floating point rounding errors in Ruby, and I noticed the following behaviour which surprised me. First, an unsurprising example, where the rounding error occurs: numbers = Array.new(10, 0.1) #=> [0.1,…
Erik Madsen
  • 1,913
  • 3
  • 20
  • 34
5
votes
2 answers

Ruby library function to transform Enumerable to Hash

Consider this extension to Enumerable: module Enumerable def hash_on h = {} each do |e| h[yield(e)] = e end h end end It is used like so: people = [ {:name=>'fred', :age=>32}, {:name=>'barney',…
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191
5
votes
6 answers

Cool tricks and expressive snippets with ruby collections/enumerables

What are your favorite code snippets with ruby collections? Preferably they should be discovery for you, be expressive, readable and introduce some fun in your coding practice. Pattern-matching in arrays (for local variables and parameters): (a,…
Alexey
  • 9,197
  • 5
  • 64
  • 76
5
votes
1 answer

Why is Range#sum defined in Enumerable module?

In Ruby 2.4 and for Integer Ranges, Range(Enumerable)#sum is optimized to return a result directly, without iterating over all elements. I don't understand why the corresponding code is defined in enum.c for the Enumerable module and not in range.c…
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
5
votes
1 answer

Example of a recursively enumerable language that is not context free

What is a simple example of a recursively enumerable language that is not context free? My textbook is awful in providing such an example explicitly. To be clear this isn't a hmk question.
5
votes
1 answer

JavaScript: Unenumerable properties - when and why?

I have recently stumbled upon the issue of using for..in loops on arrays in JavaScript. According to the answers in this question, for..in is intended to enumerate the properties of an object, including inherited ones. If so, why do we have the…
Johan Hirsch
  • 557
  • 4
  • 21
5
votes
3 answers

Understanding Ruby Enumerable#map (with more complex blocks)

Let's say I have a function def odd_or_even n if n%2 == 0 return :even else return :odd end end And I had a simple enumerable array simple = [1,2,3,4,5] And I ran it through map, with my function, using a do-end block: simple.map do …
Justin L.
  • 13,510
  • 5
  • 48
  • 83
5
votes
3 answers

Problem in populating a dictionary using Enumerable.Range()

If I do for (int i = 0; i < appSettings.Count; i++) { string key = appSettings.Keys[i]; euFileDictionary.Add(key, appSettings[i]); } It is working fine. When I am trying the same thing using Enumerable.Range(0, appSettings.Count).Select(i…
Newbie
  • 1,111
  • 3
  • 17
  • 36
5
votes
1 answer

Differences between [1,2,3].to_enum and [1,2,3].enum_for in Ruby

In Ruby I'm trying to understand between the to_enum and enum_for methods. Before I my question, I've provided some sample code and two examples to help w/ context. Sample code: # replicates group_by method on Array class class Array def…
popedotninja
  • 1,170
  • 1
  • 12
  • 23
5
votes
3 answers

Sort a collection of objects by number (highest first) then by letter (alphabetical)

I'm building a widget to show medal counts for the Olympics. I have a collection of "country" objects, where each has a "name" attribute, and "gold", "silver", "bronze" for medal counts. List should be sorted: 1. First by total medal count 2. If…
dlehman
  • 338
  • 2
  • 8