# Simple ruby question

**URL:** <https://www.ruby-forum.com/t/simple-ruby-question/132237>\
**Category:** Rails\
**Created:** [March 18, 2008, 7:05am UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237 "2008-03-18T07:05:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 7:05am UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/1 "2008-03-18T07:05:59Z")

</div>

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 \*2 is giving output as “1”  
but

(1 \*\* 2).to\_s \* 2 is giving output as “11”

the above things that I tried in irb shell

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 8:11am UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/2 "2008-03-18T08:11:09Z")

</div>

(1 \*\* 2).to\_s \*2 is giving output as “1”

> but
> 
> (1 \*\* 2).to\_s \* 2 is giving output as “11”

(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

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/3 "2008-03-18T16:16:34Z")

</div>

But when I tried with “1” \*2 it is giving output as “11” and the same  
output  
I am getting even I tried with “1” \*2.

Can you please explain me clearly. I am NOT getting…

On Mon, Mar 17, 2008 at 11:10 PM, dhaval [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/4 "2008-03-18T16:33:53Z")

</div>

the \* is both a “unary unarray” operator and a binary multiplication  
operator.

\*2 is parsed as the former: (1 \*\* 2).to\_s(\*2)

- 2 is parsed as the latter: (1 \*\* 2).to\_s() \* 2

-Rob

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/5 "2008-03-18T16:46:21Z")

</div>

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??

On Mar 18, 8:32 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/6 "2008-03-18T18:11:30Z")

</div>

On Mar 18, 2008, at 11:45 AM, dhaval wrote:

> 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)

You’re looking at Numeric#to\_s and String#to\_i

-Rob

---

<div class="post-metadata">

**Author:** ![ramu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e95f7d/32.png) [@ramu](https://www.ruby-forum.com/u/ramu)\
**Post date:** [March 18, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/simple-ruby-question/132237/7 "2008-03-18T17:08:16Z")

</div>

On Mar 18, 11:32 am, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the \* is both a “unary unarray” operator and a binary multiplication  
> operator.
> 
> \*2 is parsed as the former: (1 \*\* 2).to\_s(\*2)

But 2 is not an array. Is it being coerced into a single element array  
to allow the ‘unary unarray’ to proceed w/o error?
