enumerable

Trying to write my own all? method in Ruby

喜欢而已 提交于 2019-12-13 08:30:18
问题 There is a method called all? in Enumerable. I'm trying to learn all the methods of Enumberable's library by writing them myself. This is what I've come up so far for the all? method. I sorta understand it but I got stumped when trying to pass initialized values to my method. EDIT for the record, I'm aware that enum method that I have is not the right way ie, it's hard-coded array. This is for self-learning purposes. I'm just trying to figure out how to pass the initialized values to my all?

How to add non-enumerable property in JavaScript for IE8?

我只是一个虾纸丫 提交于 2019-12-12 22:41:09
问题 Is there a way to add "hidden" non-enumerable properties to a JavaScript object that works cross-browser? For most modern browsers, you can do: Object.defineProperty(obj, '__id__', { enumerable: false, value: id++ }); For some older non-IE browsers that don't have Object.defineProperty , you can use the __proto__ hack. None of those, however, work for IE. Is there a way to accomplish this in IE8 (would be cool if IE7 too, but not necessary)? The main goal is to be able to add tracker

Should a GetEnumerator method still be idempotent when the class does not implement IEnumerable

雨燕双飞 提交于 2019-12-12 16:28:36
问题 This question piggy backs of another question which I raised regarding abusing the IEnumerable interface by modifying an object as you iterate over it. The general consensus is that no anything that Implements IEnumerable should be idempotent. But .net supports compile time duck typing with the foreach statement. Any object that provides an IEnumerator GetEnumerator() method can be used inside a foreach statement. So should the GetEnumerator method be idempotent or is it when it implements

Create List of Tuples from List using LINQ

房东的猫 提交于 2019-12-12 08:56:09
问题 I'm trying to create a list of tuples from a list using LINQ, but can't work out how to do it. What I've got is various data in an external file, which I'm reading sections using a standard method into a List<Single> , I then need to turn this into lists of groups of sequential elements of this list (which may have a variable number of elements in the groups). To state it another way: List<Single> with n elements goes to List<Tuple<Single, Single>> with (n/2) elements or List<Single> with n

NotSupportedException on IQuery's Enumerable when using statelesssession

梦想的初衷 提交于 2019-12-11 08:36:42
问题 when trying to use the Enumerable method on a named query, with a Stateless session, as shown in the example at: http://www.nhforge.org/doc/nh/en/#batch-statelesssession i am seeing a NotSupportedException. the stack trace is as below: System.NotSupportedException: Specified method is not supported. at NHibernate.Impl.StatelessSessionImpl.Enumerable(String query, QueryParameters parameters) at NHibernate.Impl.QueryImpl.Enumerable() here is a snippet of my code: IStatelessSession

Ruby: Array to hash, without any local variables

心不动则不痛 提交于 2019-12-11 03:58:26
问题 I have an array of strings. array = ["foo","bar","baz"] What I'm trying to transform this into is the following: {"foo"=>nil, "bar"=>nil, "baz" => nil} I've been doing this with the following: new_hash = {} array.each { |k| new_hash[k] = nil } new_hash I was wondering if there's any way to accomplish this in a one-liner / without any instance variables. 回答1: This would work: new_hash = Hash[array.zip] # => {"foo"=>nil, "bar"=>nil, "baz"=>nil} array.zip returns [["foo"], ["bar"], ["baz"]] Hash

Ruby Enumeration: Taken first n where block returns true

落花浮王杯 提交于 2019-12-10 21:19:20
问题 I want to take the first "n" entries which pass the block a = 1..100_000_000 # Basically a long array # This iterates over the whole array -- no good b = a.select{|x| x.expensive_operation?}.take(n) I want to short circuit the iteration once i've got n entries where 'expensive' condition is true. What do you suggest? take_while and keep count of n? # This is the code i have; which i think can be written better, but how? a = 1..100_000_000 # Basically a long array n = 20 i = 0 b = a.take_while

How to traverse this hash within one line?

☆樱花仙子☆ 提交于 2019-12-10 19:01:31
问题 Each key in a hash has a value that's also a hash. { 100 => { 1 => 'ruby', 2 => 'enumerables' }, 50 => { 3 => 'can', 4 => 'cause' }, 15 => { 5 => 'occassional', 6 => 'insanity' } } For each hash object, I want to discard the top-level key, and replace it with the key and value of the nested hash objects. { 1 => 'ruby', 2 => 'enumerables', 3 => 'can', 4 => 'cause', 5 => 'occasional', 6 => 'insanity' } I have it working, but my method uses a merge! , and requires creating another hash to store

When is custom enumerable/collection useful?

99封情书 提交于 2019-12-10 18:15:08
问题 I am dropping this line after having visited different websites to try understand real time example of using custom enumeration. I got examples. But they lead me to confusion. Example Take 1 class NumberArray { public int[] scores; public NumberArray() { } public NumberArray(int[] scores) { this.scores = scores; } public int[] Scores { get {return scores;} } } Take 2 public class Enumerator : IEnumerator { int[] scores; int cur; public Enumerator(int[] scores) { this.scores = scores; cur = -1

Rails - Enumerable Group_By multiple associations

假如想象 提交于 2019-12-10 11:16:16
问题 I want to group a collection of objects by their has many relations... like this s.inventoryitems.group_by{|i| i.locations} For the sake of simplicity this returns me something like this: {[1, 2, 3]=>["a"], [2]=>["b", "c"], []=>["d"]} I'm looking for a result like this though: {[1] => ["a"], [2] => ["a","b","c"], [3] => ["a"], [] => ["d"]} I am working on restructuring things so this can all be done in a more intuitive DB & model association oriented way, but in the meantime I need implement