You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
328 B
Ruby
20 lines
328 B
Ruby
class Parser
|
|
def initialize(pattern)
|
|
@pattern = pattern
|
|
end
|
|
|
|
def parse(stream)
|
|
a = stream.chars
|
|
b = a.shift
|
|
if b =~ @pattern
|
|
return [b, parse(a.join)]
|
|
end
|
|
end
|
|
end
|
|
|
|
parser = ->(str) {
|
|
parser.call [str[1].flatten.chars.first, str[1].flatten.chars.drop(1)]
|
|
}
|
|
|
|
p parser.call ["", "Hello, World"]
|