From: Slark Date: 2008-03-19T21:54:55+09:00 Subject: Re: Undefined method '<' for nil:NilClasse 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. A simple way to understand how scoping (and naming convention) is working in ruby is to try the following 3 examples and note their output/errors: 1.... class C str = "Hello" def look puts str.class end end c = C.new c.look 2.... class C @str = "Hello" def look puts @str.class end end c = C.new c.look 3... class C @@str = "Hello" def look puts @@str.class end end c = C.new c.look Graham