# Please explain that

**URL:** <https://www.ruby-forum.com/t/please-explain-that/148214>\
**Category:** Ruby\
**Created:** [September 10, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214 "2008-09-10T15:31:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 3:31pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/1 "2008-09-10T15:31:41Z")

</div>

Can you please explain this behaviour ?

irb(main):017:0\> 071.to\_s  
=\> “57”  
irb(main):018:0\> 71.to\_s  
=\> “71”

Why is an octal Number converted to decimal when outputted as String ?

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 3:39pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/2 "2008-09-10T15:39:19Z")

</div>

On Wed, Sep 10, 2008 at 3:24 PM, Jan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you please explain this behaviour ?
> 
> irb(main):017:0\> 071.to\_s

On the one hand a leading “0” in an integer literal like that tells  
Ruby you are using base 8. On the other hand #to\_s prints in base 10  
(by default). You’ve got the same number, only written in different  
ways.

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 3:39pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/3 "2008-09-10T15:39:54Z")

</div>

> Can you please explain this behaviour ?
> 
> irb(main):017:0\> 071.to\_s  
> =\> “57”  
> irb(main):018:0\> 71.to\_s  
> =\> “71”
> 
> Why is an octal Number converted to decimal when outputted as String ?

Let’s take a look at ri Fixnum#to\_s:

## fix.to\_s( base=10 ) -\> aString

```
 Returns a string containing the representation of _fix_ radix
 _base_ (between 2 and 36).

```

Here you have it - default base is 10.

Regards,  
Rimantas

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/4 "2008-09-10T15:42:16Z")

</div>

On Wed, Sep 10, 2008 at 15:24, Jan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you please explain this behaviour ?
> 
> irb(main):017:0\> 071.to\_s  
> =\> “57”  
> irb(main):018:0\> 71.to\_s  
> =\> “71”
> 
> Why is an octal Number converted to decimal when outputted as String ?

It’s not an octal number anymore when it’s parsed. It’s just a number.

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/5 "2008-09-10T15:41:05Z")

</div>

On Wed, Sep 10, 2008 at 9:24 AM, Jan P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can you please explain this behaviour ?
> 
> irb(main):017:0\> 071.to\_s  
> =\> “57”  
> irb(main):018:0\> 71.to\_s  
> =\> “71”
> 
> Why is an octal Number converted to decimal when outputted as String ?

Leave off the to\_s:

> > 071  
> > =\> 57

You can represent your numbers in octal by putting a leading 0, but  
they will be accessed as decimal numbers throughout your application.  
If you want to display (any) number as octal, just use:

> > 071.to\_s(8)  
> > =\> “71”  
> > 57.to\_s(8)  
> > =\> “71”

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 4:17pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/6 "2008-09-10T16:17:01Z")

</div>

On Wed, Sep 10, 2008 at 10:24:18PM +0900, Jan P. wrote:

> Can you please explain this behaviour ?
> 
> irb(main):017:0\> 071.to\_s  
> =\> “57”  
> irb(main):018:0\> 71.to\_s  
> =\> “71”
> 
> Why is an octal Number converted to decimal when outputted as String ?

Check the docs for Fixnum#to\_s. It takes a base argument, which defaults  
to  
10. Also, I’ll point out that a number isn’t octal or decimal or  
anything  
else. It can be represented in whatever base, but it’s just a number.

–Greg

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/7 "2008-09-10T16:46:22Z")

</div>

On Wed, Sep 10, 2008 at 3:33 PM, Gregory B.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can represent your numbers in octal by putting a leading 0, but  
> they will be accessed as decimal numbers throughout your application.

In fact the application itself has no sense of bases, applications  
deal with numbers. Bases enter the game in literals, printing, etc.

---

<div class="post-metadata">

**Author:** ![janpipek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b5e925/32.png) [@janpipek](https://www.ruby-forum.com/u/janpipek)\
**Post date:** [September 10, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/please-explain-that/148214/8 "2008-09-10T17:05:11Z")

</div>

On Wed, Sep 10, 2008 at 9:47 AM, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, Sep 10, 2008 at 3:33 PM, Gregory B.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You can represent your numbers in octal by putting a leading 0, but  
> > they will be accessed as decimal numbers throughout your application.
> 
> In fact the application itself has no sense of bases, applications  
> deal with numbers. Bases enter the game in literals, printing, etc.

Right, I was mainly referring to the defaults for interacting with  
numbers, but I could have been more clear.

-greg
