Continuation...
So,
"12\^ abc23 44".scan(/\d+/)
"12\^ abc23 44".scan(/\d+/).map { |e| e }
"12\^ abc23 44".enum_for(:scan, /\d+/).map { |e| e }
"12\^ abc23 44".enum_for(:scan, /\d+/).map.to_a
"12\^ abc23 44".enum_for(:scan, /\d+/).enum_for(:map).to_a... are pretty equivalent (in results term) and you should use the
first form,
There are use cases for #enum_for though - these are typically related
to situations where the set of items generated may be large. Note
also that many methods implicitly do #enum_for if no block is passed,
e.g. Integer#times does this:
irb(main):008:0> x = 5.times
=> #<Enumerator: 5:times>
irb(main):009:0> x.class
=> Enumerator
irb(main):010:0> x.class.ancestors
=> [Enumerator, Enumerable, Object, Kernel, BasicObject]
irb(main):011:0> x.each {|i| p i}
0
1
2
3
4
=> 5
It seems I am not able to clear myself to you people. I know how #scan works.
But question is while I am writing"any string".enum_for(:scan, /\d+/) --- then which version of block is
actually called, *block version* or *without block* version. Because I am not
able to see it,If I write "any string".scan(/\d+/) --- It is clear that I am using without
block version.Now If I write "any string".scan(/\d+/) { |m| p m } - It is clear I am using
block version.Now, while I write - "any string".enum_for(:scan, /\d+/) -- I can't see how
$scan is actually working. So my question came out right there...Hope it is clear now.
Yes. And you can test this yourself fairly easily:
irb(main):001:0> class X; def a; printf "block: %s\n", block_given? end end
=> nil
irb(main):002:0> X.new.enum_for(:a).each {|x| p x}
block: true
=> nil
If the method actually invokes the block this is what happens:
irb(main):003:0> class X; def a; printf "block: %s\n", block_given?;
yield 123 if block_given? end end
=> nil
irb(main):004:0> X.new.enum_for(:a).each {|x| p x}
block: true
123
=> 123
Kind regards
robert
ยทยทยท
On Sun, Jul 6, 2014 at 1:27 PM, Arup Rakshit <aruprakshit@rocketmail.com> wrote:
On Sunday, July 06, 2014 08:41:34 AM Abinoam Jr. wrote:
--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/