4 ms·
I'm not familiar with LINQ, but is that roughly equivalent to grains.select{|g| g.is_a?(Salt) && g.value.blank? }.sort{|a,b| a.irrelevance
by cubicle67 15y ago
I'm not familiar with LINQ, but is that roughly equivalent to
grains.select{|g|
g.is_a?(Salt) && g.value.blank?
}.sort{|a,b|
a.irrelevance <=> b.irrelevance
}.first
- BobTurbo 15y agovar saltGrains = new List<Salt>(); foreach (var g in grains) { if (g.TypeOf() == "Salt" && g.Value == nothing) saltGrains.Add(g); } saltGrains.sort(m => m.Value.irellevance); return saltGrains.First(); or something.
- jeltz 15y agoRuby tip: Instead of .sort you can use .sort_by. grains.select{|g| g.is_a?(Salt) && g.value.nil? }.sort_by(&:irrelevance).first Edit: Changed from blank? to nil? since that is probably closer to nothing in C#.
- cubicle67 15y agothanks. I've been writing Ruby for a few years now and I never knew about sort_by. Looks like is works in 1.8 too, but without the &: syntax sort_by{|g|g.irrelevance} source: http://www.ruby-doc.org/core-1.9.2/Enumerable.html#method-i-sort http://www.ruby-doc.org/core-1.9.2/Enumerable.html#method-i-...
- jeltz 15y agoI actually believe the &:symbol syntax was added in 1.8.7 since I am 100% positive we use that in our codebase which still has not been ported to 1.9. $ ruby -e 'puts VERSION; p ["10", "2", "3"].sort_by(&:to_i)' 1.8.7 ["2", "3", "10"]
- koenigdavidmj 15y agoRails adds it too, even if your version of Ruby does not support it. Yay monkeypatching! EDIT: said duck typing first. It's too early for thinking.