From: mutahhir hayat Date: 2008-03-19T15:59:03+09:00 Subject: Re: Undefined method '<' for nil:NilClasse You have to initialize instance members inside the initialize function, or another function that's called after instantiation. That's why @kounter is nil. Try putting @kounter=0 inside #initialize HTH MT On Wed, Mar 19, 2008 at 2:38 PM, Roberto Casadei wrote: > 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 @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. > -- > Posted via http://www.ruby-forum.com/. > >