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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Da22709962fc0cf31068e779c78ab6be?d=identicon&s=25 Raveendran .P (jazzezravi)
on 2009-02-28 13:15
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-ar...)

@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
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2009-02-28 14:09
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-ar...)
>
> @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
This topic is locked and can not be replied to.