enumerable

Skip over iteration in Enumerable#collect

不问归期 提交于 2019-11-28 16:12:34
问题 (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 condition is met. Is this possible without calling delete_if { |x| x == nil } on the returned array? (Using Ruby 1.8.7; my code excerpt is heavily abstracted) 回答1: There is method Enumerable#reject which serves just the purpose: (1..4).reject{|x| x == 3}.collect{|x

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

别等时光非礼了梦想. 提交于 2019-11-28 08:48:38
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 same medals, sub-sort by type (gold > silver > bronze, ie. two golds > 1 gold + 1 silver) 3. If same medals and type, sub-sort alphabetically I'm doing this in ruby, but I suppose the language doesn't matter. I did figure out a solution, but if feels like there must be a much more elegant way to do it. Here's what I did: Create a virtual attribute with

Why is Enumerable#each_with_object deprecated?

自闭症网瘾萝莉.ら 提交于 2019-11-28 07:40:33
According ApiDock , the Ruby method Enumerable#each_with_object is deprecated. Unless it's mistaken (saying "deprecated on the latest stable version of Rails" makes me suspicious that maybe it's Rails' monkey-patching that's deprecated), why is it deprecated? Well, that seems a bit weird. Even Agile Rails writes somewhere : "The Ruby 1.9 each_with_object method was found to be so handy that the Rails crew backported it to Ruby 1.8 for you". Seems like an error in apidock ? I don't see any reason why it would be :/ sawa This is rather an answer to a denial of the presupposition of your question

Why will ES6 WeakMap's not be enumerable?

六月ゝ 毕业季﹏ 提交于 2019-11-27 21:10:45
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 the WeakMap specifically has no .keys() or .values() . The AS3 version allowed us to rig some really interesting and usefull constructs but I feel the JS version is somewhat limited. Why is that? If the Flash VM could do it then what is keeping browsers from doing same? I read how it would be 'non-deterministic' but that is sort of the point right?

What are the benefits of making properties non-enumerable?

会有一股神秘感。 提交于 2019-11-27 18:28:54
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 non-enumerable, but what are the benefit of property hiding? Can we access non-enumerable properties? If yes, then what is the benefit of making them non-enumerable? Are all predefined properties of Objects set as non-enumerable? Such as the case of Array's pop and push properties being non-enumerable? I think the main benefit is to be able to

Do LINQ's Enumerable Methods Maintain Relative Order of Elements?

北城以北 提交于 2019-11-27 18:03:44
问题 Say I have List<Foo> foos where the current order of elements is important. If I then apply a LINQ Enumerable method such as GroupBy , Where or Select , can I rely on the resulting IEnumerable<Foo> to iterate in the same relative order as the original list? 回答1: Yes, for Enumerable methods (LINQ to Objects, which applies to List<T> you mentioned), you can rely on the order of elements returned by Select , Where , or GroupBy . This is not the case for things that are inherently unordered like

How do you find a min / max with Ruby?

三世轮回 提交于 2019-11-27 16:50:35
I want to do something simple and straightforward, like min(5,10) , or Math.max(4,7) . Are there functions to this effect in Ruby? theIV You can do [5, 10].min or [4, 7].max They come from the Enumerable module , so anything that includes Enumerable will have those methods available. v2.4 introduces own Array#min and Array#max , which are way faster than Enumerable's methods because they skip calling #each . EDIT @nicholasklick mentions another option, Enumerable#minmax , but this time returning an array of [min, max] . [4, 5, 7, 10].minmax => [4, 10] Diego Dias You can use [5,10].min or [4,7]

Is there an inverse 'member?' method in ruby?

我只是一个虾纸丫 提交于 2019-11-27 15:10:24
I often find myself checking if some value belongs to some set. As I understand, people normally use Enumerable#member? for this. end_index = ['.', ','].member?(word[-1]) ? -3 : -2 However, this feels a little less elegant than most of things in Ruby. I'd rather write this code as end_index = word[-1].is_in?('.', ',') ? -3 : -2 but I fail to find such method. Does it even exist? If not, any ideas as to why? Vasiliy Ermolovich Not in ruby but in ActiveSupport : characters = ["Konata", "Kagami", "Tsukasa"] "Konata".in?(characters) # => true You can easily define it along this line: class Object

Random array using LINQ and C#

拥有回忆 提交于 2019-11-27 09:15:43
I was reading an article on MSDN Magazine about using the Enumerable class in LINQ to generate a random array. The article uses VB.NET and I'm not immediately sure what the equivalent is in C#: Dim rnd As New System.Random() Dim numbers = Enumerable.Range(1, 100). _ OrderBy(Function() rnd.Next) The Developer Fusion VB.Net to C# converter says that the equivalent C# code is: System.Random rnd = new System.Random(); IEnumerable<int> numbers = Enumerable.Range(1, 100).OrderBy(r => rnd.Next()); For future reference, they also have a C# to VB.Net converter . There are several other tools available

Why is Enumerable#each_with_object deprecated?

风流意气都作罢 提交于 2019-11-27 05:46:12
问题 According ApiDock, the Ruby method Enumerable#each_with_object is deprecated. Unless it's mistaken (saying "deprecated on the latest stable version of Rails" makes me suspicious that maybe it's Rails' monkey-patching that's deprecated), why is it deprecated? 回答1: Well, that seems a bit weird. Even Agile Rails writes somewhere : "The Ruby 1.9 each_with_object method was found to be so handy that the Rails crew backported it to Ruby 1.8 for you". Seems like an error in apidock ? I don't see any