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

Default Logical Sort on Resultset Array RubyonRails

I've add a custom sort to an ActiveRecord model by defining a method like this: class MyClass < ActiveRecord::Base belongs_to :parent_model #this would be the many in a has_many relationship def <=>(other) self.att <=> other.att …
tmo256
  • 936
  • 8
  • 15
0
votes
2 answers

Ruby Rails 3.2 How to group_by by an hash of array of attributes?

I have a model Defect with attribute found_in. I have a hash test_phases whose keys are various test phases and whose value is an array of found_in values. Is there a way to group the Defect by test_phases? something like…
user2371769
  • 47
  • 1
  • 5
0
votes
1 answer

Rails group_by without multiple database calls

I have the following method in my concern: def all_calculated_stats(sport, group = false) calculated_stats = Stat.calculated(sport.id, id) calculated_stats = calculated_stats.group_by { |stat| stat.stat_type.stat_type_category.name } if…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
0
votes
1 answer

ruby enumerables: is there a detect for results of block evaluation?

I'm looking for something similar to #detect in enumerables, but not quite. This is what enumerable does: [1, 2, 3].detect {|i| i > 1 } #=> 2 it returns the first instance of the array which matches the condition. Now, my purpose is to return the…
ChuckE
  • 5,610
  • 4
  • 31
  • 59
0
votes
2 answers

In a set of hashes, how to access all elements of one key as enumerable object?

I have a nested hash inside a set, in my rails app, and I'm trying to access all the values of one key in an enumerable way. So I have a set which looks like this (not the actual names of my keys and values) my_set=[{:foo=>"lion", :boolean1=>true,…
ctaymor
  • 175
  • 1
  • 13
0
votes
3 answers

Ruby way of summing up dictionary values

I have something like this: a = [{"group_id" => 1, "student_id" => 3, "candies" => 4}, {"group_id" => 2, "student_id" => 1, "candies" => 3}, {"group_id" => 1, "student_id" => 2, "candies" => 2}, {"group_id" => 3, "student_id" => 4, "candies"…
shashydhar
  • 801
  • 3
  • 8
  • 26
0
votes
4 answers

Union/Except on lists with different object instances

Is it possible to perform union/except on Lists of Objects where the instance of objects are not necessarily the same but they are functionally equivalent? What I mean is if I have a class like this, Class A { String a; int b; double…
TtT23
  • 6,876
  • 34
  • 103
  • 174
0
votes
1 answer

Properties of recursive languages

Can anybody please help me with these questions? Which properties of the following recursive languages are recursively enumerable? L_1 = { L | L is regular} L_2 = { L | L contains < A > for some A,which holds on sequence 000111000} L_3 = { L | L…
0
votes
2 answers

Ruby --- custom #to_a automatic completion for excel-spreadsheet style alphanumeric coordinates

In ruby I know you can do ("A".."D").to_a and get #=> ["A", "B", "C", "D"], and the same with numbers. However, I'm trying to do something like this: ("19B".."23B").to_a #=> ["19B", "20B", "21B", "22B", "23B"] Ruby appears to be "getting it" to a…
coloradoblue
  • 625
  • 7
  • 11
0
votes
3 answers

Apply method to all elements in enumerable with LINQ

I have a list, trying to accomplish the following. I want to run a mapper method for each item in the list...can't seem to get the syntax correct var viewModelList = result.MyEnumerable.Select(MyMapper(item goes here)) public static MyViewModel…
newbie_86
  • 4,520
  • 17
  • 58
  • 89
0
votes
2 answers

How is an Enumerable converted to a Dictionary?

I have the following code from MSDN sample: if (sheetData.Elements().Where(r => r.RowIndex == rowIndex).Count() != 0) { row = sheetData.Elements().Where(r => r.RowIndex == rowIndex).First(); ... Which I refactored as…
Chibueze Opata
  • 9,856
  • 7
  • 42
  • 65
0
votes
2 answers

Looking for proper container / functions for enumerable in c++

I am trying to convert some code from c# to c++ but lack of dictionary tables/enumerables etc making me difficult to get the result needed in c++. Can anyone help with the type of container/methods to use in c++ to get the result needed? Thanks in…
ejuser
  • 181
  • 2
  • 5
  • 13
0
votes
2 answers

Get length of array property in internal class in C#, or other iteration method that works?

I have a set of C# classes generated by the json2charp web utility from a JSON response resulting from a REST call. I use the classes to deserialize future JSON responses into those classes. Everything works great. One of the internal classes has…
Robert Oschler
  • 14,153
  • 18
  • 94
  • 227
0
votes
2 answers

How to check if an appropriate enumerable is a palindrome

Obviously, a hash would not work for this kind of test. Anyways, here's what I have so far: module Enumerable def palindrome? arr = [] self.reverse_each do |x| arr << x end self == arr end end Another idea I had was to…
Rishi
  • 945
  • 2
  • 15
  • 23
0
votes
1 answer

Do I need an Enumerator for this?

I want to do this: