Project

General

Profile

Actions

Feature #3767

closed

String#scan enumerator?

Added by trans (Thomas Sawyer) over 13 years ago. Updated almost 13 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:31954]

Description

=begin
Should String#scan return an Enumerator if no block is given?

I came across this issue when testing a String extension I have that works in 1.8, but not 1.9 b/c String is no longer enumerable.

class String

 # Returns an Enumerator for iterating over each
 # line of the string, stripped of whitespace on
 # either side.
 #
 def cleanlines(&block)
   if block
     scan(/^.*?$/) do |line|
       block.call(line.strip)
     end
   else
     Enumerator.new(self) do |output|
       scan(/^.*?$/) do |line|
         output.yield(line.strip)
       end
     end
   end
 end

end
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0