I am learning Ruby and when I am trying to work on some sample examples I got some strange (may be to me) output can you please explain me why it is ??
(1**2).to_s results in 1.to_s *2 gives "1" becoz it is interpreted as
radix that is representation of 1 in base 2
the other bit is pretty simple i mean the output as "11"
I hope that clears your question
I would agree with bob but basically when you take a look at source
code of that then i figured out that if * is not passed even then it
would behave in the same way.
(1**2).to_s(2)
this would still give 1
but there is some wierd thing going on with to_i
when you say "3".to_i(10) outputs 3 with base 10 which is as expected
but
if you say "3".to_i(2) outputs 0 rather than binary representation of
3. any idea??
I would agree with bob but basically when you take a look at source
code of that then i figured out that if * is not passed even then it
would behave in the same way.
(1**2).to_s(2)
this would still give 1
but there is some wierd thing going on with to_i
when you say "3".to_i(10) outputs 3 with base 10 which is as expected
but
if you say "3".to_i(2) outputs 0 rather than binary representation of
3. any idea??
str.to_i(base) says to interpret 'str' as a representation of a number
in base 'base'. Since "3" doesn't contain any valid base 2 digits
[0,1], the value is 0 (just like with "hello".to_i)