Implicit Conversion

Discussion in 'Ruby' started by Demetirck Demetrickn, Nov 19, 2008.

  1. 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 ?
    --
    Posted via http://www.ruby-forum.com/.
     
    Demetirck Demetrickn, Nov 19, 2008
    #1
    1. Advertising

  2. Demetirck Demetrickn wrote:
    > 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
    --
    NP: In Flames - Sober And Irrelevant
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Nov 19, 2008
    #2
    1. Advertising

  3. Demetirck Demetrickn

    Alan Johnson Guest

    On Wed, Nov 19, 2008 at 12:35 PM, Demetirck Demetrickn
    <> wrote:
    > 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 ?
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    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.
     
    Alan Johnson, Nov 19, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob
    Replies:
    7
    Views:
    422
    Dale King
    Oct 3, 2003
  2. dmoos AT esigma-systems DOT de

    implicit vs. explicit type conversion for string vs. (char *)

    dmoos AT esigma-systems DOT de, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    2,537
    Default User
    Jun 26, 2003
  3. Russell Reagan

    Implicit conversion is evil?

    Russell Reagan, Oct 27, 2003, in forum: C++
    Replies:
    2
    Views:
    328
    Russell Reagan
    Oct 27, 2003
  4. al
    Replies:
    2
    Views:
    5,626
    Ernst Murnleitner
    Jan 7, 2004
  5. Boris
    Replies:
    3
    Views:
    421
Loading...

Share This Page