enumerable

Possible to access the index in a Hash each loop?

假如想象 提交于 2019-12-20 08:06:00
问题 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 iteration this is # (without having to create a count variable)? } 回答1: If you like to know Index of each iteration you could use .each_with_index hash.each_with_index { |(key,value),index| ... } 回答2: You could iterate over the keys, and get the values out

How can I get a list from a Ruby enumerable?

北战南征 提交于 2019-12-19 21:41:23
问题 I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby? I know of : elements = [] enumerable.each {|i| elements << i} I also know of the inject alternative. Is there some ready available method? 回答1: Enumerable#to_a 回答2: If you want to do some transformation on all the elements in your enumerable, the #collect (a.k.a. #map) method would be helpful: elements = enumerable.collect { |item| item.to_s } In this example,

Enumerable giving unexpected output

六眼飞鱼酱① 提交于 2019-12-19 20:37:57
问题 class Foo { public static IEnumerable<int> Range(int start, int end) { return Enumerable.Range(start, end); } public static void PrintRange(IEnumerable<int> r) { foreach (var item in r) { Console.Write(" {0} ", item); } Console.WriteLine(); } } class Program { static void TestFoo() { Foo.PrintRange(Foo.Range(10, 20)); } static void Main() { TestFoo(); } } Expected Output: 10 11 12 13 14 15 16 17 18 19 20 Actual Output: 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 What is the

Ruby block taking array or multiple parameters

最后都变了- 提交于 2019-12-19 17:40:11
问题 Today I was surprised to find ruby automatically find the values of an array given as a block parameter. For example: foo = "foo" bar = "bar" p foo.chars.zip(bar.chars).map { |pair| pair }.first #=> ["f", "b"] p foo.chars.zip(bar.chars).map { |a, b| "#{a},#{b}" }.first #=> "f,b" p foo.chars.zip(bar.chars).map { |a, b,c| "#{a},#{b},#{c}" }.first #=> "f,b," I would have expected the last two examples to give some sort of error. Is this an example of a more general concept in ruby? I don't think

Does Enumerable's group_by preserve the Enumerable's order?

一笑奈何 提交于 2019-12-18 14:56:22
问题 Does Enumerable#group_by preserve the original order within each value? When I get this: [1, 2, 3, 4, 5].group_by{|i| i % 2} # => {1=>[1, 3, 5], 0=>[2, 4]} is it guaranteed that, for example, the array [1, 3, 5] contains the elements in this order and not, for example [3, 1, 5] ? Is there any description regarding this point? I am not mentioning the order between the keys 1 and 0 . That is a different issue. 回答1: Yes, Enumerable#group_by preserves input order. Here's the implementation of

Executing a certain action for all elements in an Enumerable<T>

僤鯓⒐⒋嵵緔 提交于 2019-12-18 13:51:11
问题 I have an Enumerable<T> 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 => GenHTMLOutput(s)); // (where GenHTMLOutput cannot for some reason receive the enumerable itself as a parameter) I did try Select(s=> { Console.WriteLine(s); return s; }) , but it wasn't printing anything. 回答1: A quick-and-easy way to get this is:

Is there anything like Enumerable.Range(x,y) in Java?

那年仲夏 提交于 2019-12-18 07:39:19
问题 Is there something like C#/.NET's IEnumerable<int> range = Enumerable.Range(0, 100); //.NET in Java? 回答1: Edited: As Java 8, this is possible with java.util.stream.IntStream.range(int startInclusive, int endExclusive) Before Java8: There is not such thing in Java but you can have something like this: import java.util.Iterator; public class Range implements Iterable<Integer> { private int min; private int count; public Range(int min, int count) { this.min = min; this.count = count; } public

Apply method to each elements in array/enumerable

你。 提交于 2019-12-17 22:43:56
问题 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)? 回答1: This will work: array.map!(&:to_s) 回答2: You can use map or map! respectively, the first will return a new list, the second will modify the list in-place: >> array = [:one,:two,:three] => [:one, :two, :three] >> array.map{ |x| x.to_s } => ["one", "two", "three"] 回答3: It's

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

寵の児 提交于 2019-12-17 18:44:13
问题 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

Array#each vs. Array#map

Deadly 提交于 2019-12-17 02:19:18
问题 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 1 uses vs.map , case 2 uses vs.each . What happened here? 回答1: Array#each executes the given block for each element of the array, then returns the array itself. Array#map also executes the given block for each element of the array, but returns a new