Split a string based on change of character

P

Peña, Botp

From: Logan Capaldo [mailto:[email protected]]=20
# > Enumerator.new(s, :scan, /(.)\1*/).map {$&}
# What's wrong with s.enum_for:)scan, /(.)\1*/).map { $& } ?

somehow i missed the enumerator hack. thank you logan and brad for the =
update.
kind regards -botp
 
B

Brad Phelan

Peña said:
From: Logan Capaldo [mailto:[email protected]]
# > Enumerator.new(s, :scan, /(.)\1*/).map {$&}
# What's wrong with s.enum_for:)scan, /(.)\1*/).map { $& } ?

somehow i missed the enumerator hack. thank you logan and brad for the update.
kind regards -botp

Hey cool .. 'enum_for' exactly what I was looking for. I couldn't
understand why it didn't exist and it does. Scratch my suggestion
for 'enum_scan'.

B
 
B

Brad Phelan

Brad said:
Peña said:
From: Logan Capaldo [mailto:[email protected]] # On 8/13/07, Brad
# > Enumerator.new(s, :scan, /(.)\1*/).map {$&}
# What's wrong with s.enum_for:)scan, /(.)\1*/).map { $& } ?

somehow i missed the enumerator hack. thank you logan and brad for the
update.
kind regards -botp

Hey cool .. 'enum_for' exactly what I was looking for. I couldn't
understand why it didn't exist and it does. Scratch my suggestion
for 'enum_scan'.

B

Would it not be clearer if enum_for worked as

s.enum_for.scan(/(.)\1*/).map { $& }

Quickie


require 'enumerator'

module Enumerable
class Expr
def initialize( enum )
@enum = enum
end

def method_missing(name, *args)
@enum.enum_for_old(name, *args)
end
end

end

class Object
alias :enum_for_old :enum_for
def enum_for
Enumerable::Expr.new(self)
end
end


s = "aabbccvvvvfg dddd"
r = s.enum_for.scan(/(.)\1*/).map {$&}
puts r


(To be correct though a full implementation should use the BlankSlate
class and properly implement the original enum_for interface )

B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top