+ 2

Ruby - Why these values for a[0] and a[1]?

a = "3 is the answer 4 is not".to_i b = a[1]*a[0]+a[1] puts b*b puts a[0] puts a[1] puts a

24th Apr 2019, 4:20 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
1 Answer
+ 11
to_i gets first number of the string. So, a = 3 3 in binary is 00000011 First position is the 1 bit at the right. Second position is penultimate bit So a[0] = 1 ( 00000001) a[1] = 1 (00000010) a[2] = 0 (00000000)
24th Apr 2019, 5:38 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar