Implicit Conversion

  • Thread starter Demetirck Demetrickn
  • Start date
D

Demetirck Demetrickn

Hello all,
I'm new to this site I'm an intern at software company and was given the
task of writing a program in ruby that simply could open data files
check for a particular field in the text file and compare the intger
agains't a mininum value.

I seem to be getting a prblem from ruby that says
" `[]': no implicit conversion from nil to integer (TypeError)"

Ruby doesn't seem to let me give integer values to an array inside a
loop.
Is there anyway I can static cast or do a type conversion in ruby ?
 
S

Sebastian Hungerecker

Demetirck said:
I seem to be getting a prblem from ruby that says
" `[]': no implicit conversion from nil to integer (TypeError)"

Ruby doesn't seem to let me give integer values to an array inside a
loop.

That error doesn't happen when you give an integer, it happens when you give
nil. array[1] works, array[nil] does not. So likely you have a variable that
is nil or a method that returns nil, while you expect it to return an integer.
(For example Regex#=~ returns nil if the regex does not match. So if you use
the return value from =~, you'll have to check that it's not nil before using
it in []).

HTH,
Sebastian
 
A

Alan Johnson

Hello all,
I'm new to this site I'm an intern at software company and was given the
task of writing a program in ruby that simply could open data files
check for a particular field in the text file and compare the intger
agains't a mininum value.

I seem to be getting a prblem from ruby that says
" `[]': no implicit conversion from nil to integer (TypeError)"

Ruby doesn't seem to let me give integer values to an array inside a
loop.
Is there anyway I can static cast or do a type conversion in ruby ?

It doesn't sound like your error is necessarily what you think it is. That
error happens when you do something like:
a = [1, 2, 3, 4, 5]
i = nil
a

You can convert most objects (including nil) to an integer with the to_i
method, but that probably isn't going to fix anything. nil.to_i is always 0.

Post some code and someone here will likely be able to spot your error.
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top