Doubt in -- Ruby --> Array --> array.display -- method

Discussion in 'Ruby' started by jazzez ravi, Feb 28, 2009.

  1. jazzez ravi

    jazzez ravi Guest

    Hi All,

    I created few (array's method related) questions in ruby. But I am not
    clear about one question. Here is the question ..


    Question 7
    (from
    http://raveendran.wordpress.com/2009/02/18/ruby-array-related-excercise/)

    @a=[34,45,56,2,13,54]

    @b= @a[2].display.to_i + @a[3].display.to_i

    puts @b gives,

    a) Error b) 58 c) 5620 d) 562

    This is the question. In my view,

    MY Solution 1:

    @a[2].display.to_i => 56.display gives 56.to_i => 56
    @a[3].display.to_i => 2.diaply gives 2.to_i => 2

    @b= 56 + 2

    @b must come as 58

    MY Solution 2:

    56.display -> displays 56 -> Here .to_i will not work

    So my answers is,

    @b= 562


    But the result came as 5620 . how it works ?

    Thanks,
    P.Raveendran
    --
    Posted via http://www.ruby-forum.com/.
     
    jazzez ravi, Feb 28, 2009
    #1
    1. Advertising

  2. jazzez ravi wrote:
    > Hi All,
    >
    > I created few (array's method related) questions in ruby. But I am not
    > clear about one question. Here is the question ..
    >
    >
    > Question 7
    > (from
    > http://raveendran.wordpress.com/2009/02/18/ruby-array-related-excercise/)
    >
    > @a=[34,45,56,2,13,54]
    >
    > @b= @a[2].display.to_i + @a[3].display.to_i
    >
    > puts @b gives,
    >
    > a) Error b) 58 c) 5620 d) 562
    >
    > This is the question. In my view,

    (...)
    >
    > MY Solution 2:
    >
    > 56.display -> displays 56 -> Here .to_i will not work
    >
    > So my answers is,
    >
    > @b= 562
    >
    >
    > But the result came as 5620 . how it works ?
    >
    > Thanks,
    > P.Raveendran


    56.display -> prints 56 and returns nil.
    nil.to_i returns 0
    So @b equals zero

    hth,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Feb 28, 2009
    #2
    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. SG
    Replies:
    6
    Views:
    30,570
    ricuzzo
    Jan 15, 2008
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    641
  3. Replies:
    0
    Views:
    581
  4. Peter Otten
    Replies:
    2
    Views:
    131
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    124
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page