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

How to find a min/max with Ruby

I want to use min(5,10), or Math.max(4,7). Are there functions to this effect in Ruby?
obuzek
  • 4,933
  • 2
  • 18
  • 6
174
votes
8 answers

What does enumerable mean?

I was directed to MDN's for..in page when it said, "for..in Iterates over the enumerable properties of an object." Then I went to the Enumerability and ownership of properties page where it said "Enumerable properties are those which can be iterated…
Patrick
  • 2,176
  • 3
  • 18
  • 21
168
votes
9 answers

What is the use of Enumerable.Zip extension method in Linq?

What is the use of Enumerable.Zip extension method in Linq?
user634871
  • 1,723
  • 2
  • 11
  • 5
136
votes
7 answers

Why does Enumerable.All return true for an empty sequence?

var strs = new Collection(); bool b = strs.All(str => str == "ABC"); The code creates an empty collection of string, then tries to determine if all the elements in the collection are "ABC". If you run it, b will be true. But the collection…
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87
133
votes
2 answers

Possible to access the index in a Hash each loop?

I'm probably missing something obvious, but is there a way to access the index/count of the iteration inside a hash each loop? hash = {'three' => 'one', 'four' => 'two', 'one' => 'three'} hash.each { |key, value| # any way to know which…
Upgradingdave
  • 12,916
  • 10
  • 62
  • 72
105
votes
11 answers

What is the best way to modify a list in a 'foreach' loop?

A new feature in C# / .NET 4.0 is that you can change your enumerable in a foreach without getting the exception. See Paul Jackson's blog entry An Interesting Side-Effect of Concurrency: Removing Items from a Collection While Enumerating for…
Polo
  • 1,770
  • 4
  • 18
  • 29
102
votes
2 answers

How do I create an empty Stream in Java?

In C# I would use Enumerable.Empty(), but how do I create an empty Stream in Java?
sdgfsdh
  • 33,689
  • 26
  • 132
  • 245
100
votes
7 answers

Array#each vs. Array#map

hash = { "d" => [11, 22], "f" => [33, 44, 55] } # case 1 hash.map {|k,vs| vs.map {|v| "#{k}:#{v}"}}.join(",") => "d:11,d:22,f:33,f:44,f:55" # case 2 hash.map {|k,vs| vs.each {|v| "#{k}:#{v}"}}.join(",") => "11,22,33,44,55" only difference is case…
user612308
  • 1,813
  • 5
  • 23
  • 22
89
votes
5 answers

IEnumerable doesn't have a Count method

I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } public IEnumerable GetRuleViolations(){ //code here } Why is it that when I do .Count() above it is underlined in red? I got…
aherlambang
  • 14,290
  • 50
  • 150
  • 253
62
votes
2 answers

What is the difference between map, each, and collect?

In Ruby, is there any difference between the functionalities of each, map, and collect?
Rahul
  • 44,892
  • 25
  • 73
  • 103
62
votes
10 answers

Executing a certain action for all elements in an Enumerable

I have an Enumerable and am looking for a method that allows me to execute an action for each element, kind of like Select but then for side-effects. Something like: string[] Names = ...; Names.each(s => Console.Writeline(s)); or Names.each(s…
Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
59
votes
6 answers

Skip over iteration in Enumerable#collect

(1..4).collect do |x| next if x == 3 x + 1 end # => [2, 3, nil, 5] # desired => [2, 3, 5] If the condition for next is met, collect puts nil in the array, whereas what I'm trying to do is put no element in the returned array if the…
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
57
votes
4 answers

What are the benefits of making properties non-enumerable?

Enumerability is one of the three attributes of a property: writability, enumerability, and configurability. My questions are: What are the benefit of making properties non-enumerable in JavaScript? I know we are hiding the property by making them…
Anshul
  • 9,312
  • 11
  • 57
  • 74
53
votes
4 answers

Apply method to each elements in array/enumerable

This is my array: array = [:one,:two,:three] I want to apply to_s method to all of my array elements to get array = ['one','two','three']. How can I do this (converting each element of the enumerable to something else)?
Vladimir Tsukanov
  • 4,269
  • 8
  • 28
  • 34
49
votes
5 answers

Group hashes by keys and sum the values

I have an array of hashes: [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}] I need to use inject here I think but I've really been struggling. I want a new hash that reflects the sum of the previous hash's duplicate…
blastula
  • 637
  • 1
  • 6
  • 10
1
2 3
40 41