R
Roberto Casadei
This is the code:
class StringScanner
@str = ""
@kounter = 0
def initialize(str)
@str = str
end
def scan(arg)
res = @str.scan(arg)
return nil unless @kounter<res.length
@kounter=@kounter+1
res[@kounter-1]
end
end
str = "Watch how I soar!"
ss = StringScanner.new(str)
loop do
word = ss.scan(/\w+/) # Grab a word at a time
break if word.nil?
puts word
sep = ss.scan(/\W+/) # Grab next non-word piece
break if sep.nil?
end
And this is the result:
C:/Documents and Settings/Roby/Desktop/Apps/Ruby/prova2.rb:9:in `scan':
undefined method `<' for nil:NilClass (NoMethodError)
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:18
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:17:in `loop'
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:17
Help.
class StringScanner
@str = ""
@kounter = 0
def initialize(str)
@str = str
end
def scan(arg)
res = @str.scan(arg)
return nil unless @kounter<res.length
@kounter=@kounter+1
res[@kounter-1]
end
end
str = "Watch how I soar!"
ss = StringScanner.new(str)
loop do
word = ss.scan(/\w+/) # Grab a word at a time
break if word.nil?
puts word
sep = ss.scan(/\W+/) # Grab next non-word piece
break if sep.nil?
end
And this is the result:
C:/Documents and Settings/Roby/Desktop/Apps/Ruby/prova2.rb:9:in `scan':
undefined method `<' for nil:NilClass (NoMethodError)
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:18
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:17:in `loop'
from C:/Documents and
Settings/Roby/Desktop/Apps/Ruby/prova2.rb:17
Help.