From: Rick DeNatale Date: 2008-03-19T19:45:52+09:00 Subject: Re: Undefined method '<' for nil:NilClasse On 3/19/08, Roberto Casadei wrote: > This is the code: > class StringScanner > @str = "" > @kounter = 0 Since the previous two lines are inside a class context rather than inside an instance method, they define two class instance variables, i.e. they are instance variables of the class object named StringScanner. These two variables are never actually used in the rest of the code, much as it might seem to an untrained eye. > def initialize(str) > @str = str This creates an instance variable for the INSTANCE of StringScanner being initialized, this is an entirely different variable than the @str we saw before. > 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 > Now even if you straighten out the two variables, you are going around Robin Hood's barn to accomplish what can be easily done with: "Watch how I soar!".scan(/\w+/).each {|word| puts word} or perhaps puts "Watch how I soar!".scan(/\w+/).join("\n") -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/