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
45
votes
3 answers

What's the fastest way in Ruby to get the first enumerable element for which a block returns true?

What's the fastest way in Ruby to get the first enumerable element for which a block returns true? For example: arr = [12, 88, 107, 500] arr.select {|num| num > 100 }.first # => 107 I'd like to do this without running through the entire array, as…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
45
votes
9 answers

Transform a DataTable into Dictionary C#

I want to know how to transform a DataTable into a Dictionary. I did something like this. using System.Linq; internal Dictionary GetDict(DataTable dt) { return dt.AsEnumerable() .ToDictionary(row =>…
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
37
votes
5 answers

generic Enumeration to Iterable converter

HttpServletRequest is using a lot of java.util.Enumeration. I would like to use them in for-each, so i need to convert them into interable. this is not a problem, but I since I have more than one project needing this I need a library to do this. I…
IAdapter
  • 62,595
  • 73
  • 179
  • 242
35
votes
2 answers

Why will ES6 WeakMap's not be enumerable?

Before my re-entry in JavaScript (and related) I've done lots of ActionScript 3 and there they had a Dictionary object that had weak keys just like the upcoming WeakMap; but the AS3 version still was enumerable like a regular generic object while…
Bartvds
  • 3,340
  • 5
  • 30
  • 42
31
votes
4 answers

ruby methods that either yield or return Enumerator

in recent versions of Ruby, many methods in Enumerable return an Enumerator when they are called without a block: [1,2,3,4].map #=> # [1,2,3,4].map { |x| x*2 } #=> [2, 4, 6, 8] I want do do the same thing in my own…
levinalex
  • 5,889
  • 2
  • 34
  • 48
30
votes
3 answers

Rails lists have .first and .second – is there a .hundredth or .sixty_nineth ?

Is there a class or other extension for Rails that allows more than the first few elements in a series (and the last)? These work: [2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].fifth # ->…
New Alexandria
  • 6,951
  • 4
  • 57
  • 77
29
votes
3 answers

How do you sort an array alphabetically using sort_by in ruby?

I have an array of memberships. In each membership is a group. I need to sort this array of memberships by the name of the group. I've tried a bunch of different ways, and the latest way is this: @memberships.sort_by! { |m| m.group.name } However,…
Brian Weinreich
  • 7,692
  • 8
  • 35
  • 59
28
votes
5 answers

Passing array to function that takes either params object[] or IEnumerable

I want to pass an array of custom objects to a function like String.Join which has the following signatures: public static string Join(string separator, params Object[] values) public static string Join(string separator, IEnumerable values) If…
bouvierr
  • 3,563
  • 3
  • 27
  • 32
24
votes
3 answers

What is the effect of AsEnumerable() on a LINQ Entity?

Reading the questions here and here has given me some insight into the situation, and it seems like using the AsEnumerable is memory consuming. Is there a better way to do this LINQ and the way it is done now, is the data that comes out…
Andy
  • 2,248
  • 7
  • 34
  • 57
23
votes
3 answers

Excluding one item from list (by Index), and take all others

There is a List containing some set of numbers. Randomly I select an index, which will be processed separately (call it master). Now, I want to exclude this particular index, and get all other elements of List (call them slave). var items = new…
Ajay
  • 18,086
  • 12
  • 59
  • 105
23
votes
6 answers

Idiomatic Ruby filter for nil-or-empty?

I'm looking for a more idiomatic way to filter out nil-or-empty elements of an array. I have many methods of the form: def joined [some_method, some_other_method].compact.reject(&:empty?).join(' - ') end This will take the result of some_method…
benizi
  • 4,046
  • 5
  • 28
  • 25
22
votes
6 answers

Common Ancestor to Java Array and List

In .NET, both array and list have Enumerable as ancestor, so a method that accept Enumerable as an argument can receive both array and list as its argument. I wonder if there is a similar thing in Java?
Phương Nguyễn
  • 8,747
  • 15
  • 62
  • 96
22
votes
2 answers

Implementing List Enumerator OfType in Delphi

I am using Delphi XE to implement an enumerator that allows filtering the elements of the list by type. I have quickly assembled a test unit as follows: unit uTestList; interface uses Generics.Collections; type TListItemBase = class(TObject) …
Louis Kleiman
  • 233
  • 1
  • 3
  • 7
20
votes
9 answers

Python Equivalent to Ruby's #each_cons?

Is there a Pythonic equivalent to Ruby's #each_cons? In Ruby you can do this: array = [1,2,3,4] array.each_cons(2).to_a => [[1,2],[2,3],[3,4]]
maxhawkins
  • 878
  • 6
  • 18
20
votes
6 answers

Meaning of the word yield

Currently, I'm reading "The Well-Grounded Rubyist" by David A. Black, and I'm stuck at chapter 10.9 (Enumerators and the next dimension of enumerability). My question is about the yield method. What is the meaning of the word yield in the Ruby…
kyrylo
  • 1,691
  • 1
  • 15
  • 22
1
2
3
40 41