Yes. Make sure the value you’re storing in @pop is a Fixnum.
Or any Integer value in fact.
My guess is that @pop contains nil, so you get an error like “no method
‘times’ for nil” - it would be much clearer if you posted the actual
exception.
Hi,
After posting last night I determined that ‘pop’ was a string by
running:
puts pop.class
I then found out about the .to_i method. After applying this my issue at
least seems to be resolved. The post below at least confirms I’m on the
right track, so thanks for that.
I confess I don’t know a lot of the technical stuff about class Fixnum,
as I’ve not really needed to until now.
You should take a little time and learn a bit more detail about numbers
in Ruby. Expanding your knowledge about strings and the String class
would also be a good idea so that you understand how the classes differ.
Currently I am getting the value of ‘pop’ from the user with the
following code:
puts “please enter a population size”
pop = gets.chomp
The gets method returns a String. The chomp method is a method for
String instances that also returns a String. If the String represents a
number, such as “1234”, then you can convert that into a Fixnum or
related number type by calling to_i on the String. e.g.)
the_number = pop.to_i
-Jeremy
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.