enumerable

How to create an infinite enumerable of Times?

房东的猫 提交于 2019-12-01 00:34:02
I want to be able to have an object extend Enumerable in Ruby to be an infinite list of Mondays (for example). So it would yield: March 29, April 5, April 12...... etc How can I implement this in Ruby? In 1.9 (and probably previous versions using backports ), you can easily create enumerator: require 'date' def ndays_from(from, step=7) Enumerator.new {|y| loop { y.yield from from += step } } end e = ndays_from(Date.today) p e.take(5) #=> [#<Date: 2010-03-25 (4910561/2,0,2299161)>, #<Date: 2010-04-01 (4910575/2,0,2299161)>, #<Date: 2010-04-08 (4910589/2,0,2299161)>, #<Date: 2010-04-15 (4910603

What's the best way to return an Enumerator::Lazy when your class doesn't define #each?

此生再无相见时 提交于 2019-11-30 21:45:37
Enumerable#lazy relies on your enumerable providing an #each method. If your enumerable doesn't have an #each method you can't use #lazy . Now Kernel#enum_for and #to_enum provide the flexibility to specify an enumeration method other than #each : Kernel#enum_for(method = :each, *args) But #enum_for and friends always construct plain (non-lazy) enumerators, never Enumerator::Lazy . I see that Enumerator in Ruby 1.9.3 offers this similar form of #new: Enumerator#new(obj, method = :each, *args) Unfortunately that constructor has been completely removed in Ruby 2.0. Also I don't think it was ever

How to create an infinite enumerable of Times?

天大地大妈咪最大 提交于 2019-11-30 18:10:41
问题 I want to be able to have an object extend Enumerable in Ruby to be an infinite list of Mondays (for example). So it would yield: March 29, April 5, April 12...... etc How can I implement this in Ruby? 回答1: In 1.9 (and probably previous versions using backports ), you can easily create enumerator: require 'date' def ndays_from(from, step=7) Enumerator.new {|y| loop { y.yield from from += step } } end e = ndays_from(Date.today) p e.take(5) #=> [#<Date: 2010-03-25 (4910561/2,0,2299161)>, #<Date

Rails lists have .first and .second – is there a .hundredth or .sixty_nineth ?

谁说胖子不能爱 提交于 2019-11-30 16:58:02
Is there a class or other extension for Rails that allows more than the first few elements in a series (and the last)? These work: [2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].fifth # -> 53 [2,45,2,14,53,23,634,346,34,46,643,634,346,34,34].last # -> 34 so where is? list.sixth list.hundredth There was a time when Rails added these, but there was a lot of controversy so most were removed. The only one of this experiment that remains is Array#forty_two . You can just use square brackets: list[6] list[100] In activesupport, it does monkey patching few of these methods into Array class. If you

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

那年仲夏 提交于 2019-11-30 12:18:21
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. Yes, Enumerable#group_by preserves input order. Here's the implementation of that method in MRI, from https://github.com/ruby/ruby/blob/trunk/enum.c : static VALUE enum_group_by(VALUE obj)

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

左心房为你撑大大i 提交于 2019-11-30 10:42:24
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. A quick-and-easy way to get this is: Names.ToList().ForEach(e => ...); You are looking for the ever-elusive ForEach that currently only exists on

How to get Alternate elements using Enumerable in C#

随声附和 提交于 2019-11-30 09:51:40
问题 This is a continuation of my question: How to get reverse of a series of elements using Enumarable in C#? Now I need alternate elements only. Here is my solution using for loop: int Max = 10; int limit = 5; Dictionary<String , String> MyDict = new Dictionary<string,string>(); int j = 0; for (int i = 0; i <Max; i++) { if (i >= limit) MyDict.Add((i+1).ToString(), "None"); else MyDict.Add((i+1).ToString(), j.ToString()); j+=2; } The output is like { "1" "0"} { "2" "2"} { "3" "4"} { "4" "6"} { "5

Ruby array of hash. group_by and modify in one line

瘦欲@ 提交于 2019-11-30 08:41:54
I have an array of hashes, something like [ {:type=>"Meat", :name=>"one"}, {:type=>"Meat", :name=>"two"}, {:type=>"Fruit", :name=>"four"} ] and I want to convert it to this { "Meat" => ["one", "two"], "Fruit" => ["Four"]} I tried group_by but then i got this { "Meat" => [{:type=>"Meat", :name=>"one"}, {:type=>"Meat", :name=>"two"}], "Fruit" => [{:type=>"Fruit", :name=>"four"}] } and then I can't modify it to leave just the name and not the full hash. I need to do this in one line because is for a grouped_options_for_select on a Rails form. array.group_by{|h| h[:type]}.each{|_, v| v.replace(v

Help understanding yield and enumerators in Ruby

白昼怎懂夜的黑 提交于 2019-11-30 07:37:38
I would appreciate it if someone could help me understand the difference between using a Yielder in an Enumerator vs. just invoking yield in an Enumerator. The "Well-grounded Rubyist" suggests that one doesn't "yield from the block" but doesn't explain precisely what's going on. Thanks The Enumerator::Yielder#yield method and the Enumerator::Yielder::<< method are exactly the same . In fact, they are aliases. So, which one of those two you use, is 100% personal preference, just like Enumerable#collect and Enumerable#map or Enumerable#inject and Enumerable#reduce . It might help if you first

What's the best way to return an Enumerator::Lazy when your class doesn't define #each?

冷暖自知 提交于 2019-11-30 05:44:54
问题 Enumerable#lazy relies on your enumerable providing an #each method. If your enumerable doesn't have an #each method you can't use #lazy . Now Kernel#enum_for and #to_enum provide the flexibility to specify an enumeration method other than #each : Kernel#enum_for(method = :each, *args) But #enum_for and friends always construct plain (non-lazy) enumerators, never Enumerator::Lazy . I see that Enumerator in Ruby 1.9.3 offers this similar form of #new: Enumerator#new(obj, method = :each, *args)