Undefined method '<' for nil:NilClasse

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.
 
M

mutahhir hayat

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
 
R

Rick DeNatale

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<res.length

Again @kounter is an instance variable associated with a particular
StringScanner object, it is NOT the previously seen @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")
 
R

Rick DeNatale

The probblem is in the initializing. If you explicitly initialize
@kounter in the initialize method then it knows it's a Fixnum.

Variables aren't typed in Ruby, nor are they declared. A variable
isn't a particular class, it refers to an object, and the reference
can change over the life of the program.

@a = 1 #@a is now a fix num
@a = @a * 1.2 # And now it's a float
@a = @a.to_s # And now it's a String
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top