Fwd: Using value from variable in .times method?

Hello,

I have a variable that stores the population input by a user. I then
want to use this value in the following method:

 4.times{ @offspring<<  @board.clone }

#puts @offspring.inspect
end

I tried:

 @pop.times

but this returned a ‘no method’ error.

Is there a simple way to use the value stored in @pop in this way?

Thanks,
Jen.

John F. wrote in post #988344:

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.

Yes. Make sure the value you’re storing in @pop is a Fixnum. What’s
the value of @pop when you try to use it?

~ jf

John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/

On Sat, Mar 19, 2011 at 4:16 PM, Jen [email protected] wrote:

I confess I don’t know a lot of the technical stuff about class Fixnum, as
I’ve not really needed to until now.

Time to learn :slight_smile:

Currently I am getting the value of ‘pop’ from the user with the following
code:

puts “please enter a population size”
pop = gets.chomp

$ irb

foo = “1”
=> “1”

foo.class.name
=> “String”

bar = 1
=> 1

bar.class.name
=> “Fixnum”

bar.times { puts “cool” }
cool
=> 1

foo.times { puts “cool” }
NoMethodError: undefined method `times’ for “1”:String
from (irb):6

Hi John,

I confess I don’t know a lot of the technical stuff about class Fixnum,
as I’ve not really needed to until now.

Currently I am getting the value of ‘pop’ from the user with the
following code:

puts “please enter a population size”
pop = gets.chomp

Do I have to declare the type of pop as fixnum?

Note when I run ‘pop.inspect’ after getting the value from the user it
returns the value I input.

Thanks,
Jen.

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.

Jen.

On 03/19/2011 06:16 PM, Jen wrote:

Hi John,

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.
:slight_smile:

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs