# Finding obscure documentation

**URL:** <https://www.ruby-forum.com/t/finding-obscure-documentation/116438>\
**Category:** Ruby\
**Created:** [October 5, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438 "2007-10-05T15:29:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 5, 2007, 3:29pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/1 "2007-10-05T15:29:02Z")

</div>

I have learned that you can turn a number to octal (or whatever) this  
way:

p 123.to\_s(8).to\_i  
=\> 173

I seem to recall that there is something which will read _in_ a number  
in an arbitrary numeric base, though I forget just how to do that.

My question is, where is this and things like it documented? I have  
searched several books to no avail.

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 5, 2007, 4:03pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/2 "2007-10-05T16:03:05Z")

</div>

Quoth Lloyd L.:

> searched several books to no avail.  
> “0x123”.to\_i(16) # =\> 291

Your method of “turn[ing] a number to octal” turns it to octal in a  
string,  
then converts it back to an integer. You really want to leave off the  
.to\_i  
bit.

HTH,

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 5, 2007, 4:15pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/3 "2007-10-05T16:15:27Z")

</div>

On Oct 5, 2007, at 9:29 AM, Lloyd L. wrote:

> searched several books to no avail.  
> irb(main):001:0\> “173”.to\_i(8)  
> =\> 123

ri String#to\_i

and you don’t need the .to\_i on your example. If the base it more  
than 10, it won’t work anyway.

irb(main):002:0\> 26.to\_s(16).to\_i  
=\> 1  
irb(main):003:0\> 26.to\_s(16)  
=\> “1a”

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 5, 2007, 5:33pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/4 "2007-10-05T17:33:33Z")

</div>

Rob B. wrote:

> ri String#to\_i

I think that I found what you are talking about.

str.to\_i(base=10) =\> integer

Returns the result of interpreting leading characters in str as an  
integer base base (2, 8, 10, or 16). Extraneous characters past the end  
of a valid number are ignored. If there is not a valid number at the  
start of str, 0 is returned. This method never raises an exception.

“12345”.to\_i #=\> 12345  
“99 red balloons”.to\_i #=\> 99  
“0a”.to\_i #=\> 0  
“0a”.to\_i(16) #=\> 10  
“hello”.to\_i #=\> 0  
“1100101”.to\_i(2) #=\> 101  
“1100101”.to\_i(8) #=\> 294977  
“1100101”.to\_i(10) #=\> 1100101  
“1100101”.to\_i(16) #=\> 17826049

I will have to read this closely. Thanks!

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 5, 2007, 5:35pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/5 "2007-10-05T17:35:54Z")

</div>

- Lloyd L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (15:29) schrieb:

> I have learned that you can turn a number to octal (or whatever) this  
> way:

A number is not octal or decimal or whatever. String representations of  
number are.

> p 123.to\_s(8).to\_i  
> =\> 173

That’s useless playing around with the methods. Parsing a octal string  
representation as decimal is just broken.

> I seem to recall that there is something which will read _in_ a number  
> in an arbitrary numeric base, though I forget just how to do that.

You format a number with to\_s(base=10) and parse it with to\_i(base=10).

Thus if you have a string that contains an octal encoded number,  
str.to\_i(8) returns the number.

And if you got a strange thing the above misparsed “number”, you do  
num.to\_s(10).to\_i(8).

> My question is, where is this and things like it documented? I have  
> searched several books to no avail.

Just look for to\_i, there is no magic involved.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 6, 2007, 5:34am UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/6 "2007-10-06T05:34:56Z")

</div>

From: removed\_email\_address@domain.invalid

# I will have to read this closely. Thanks!

it would be nice if ruby docs also display related methods,

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

* * *

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

    12345.to_s #=> "12345"
    12345.to_s(2) #=> "11000000111001"
    12345.to_s(8) #=> "30071"
    12345.to_s(10) #=> "12345"
    12345.to_s(16) #=> "3039"
    12345.to_s(36) #=> "9ix"

```

something like a see also string#to\_i

> qri string.to\_i  
> ------------------------------------------------------------ String#to\_i  
> str.to\_i(base=10) =\> integer

* * *

```
 Returns the result of interpreting leading characters in _str_ as
 an integer base _base_ (2, 8, 10, or 16). Extraneous characters
 past the end of a valid number are ignored. If there is not a valid
 number at the start of _str_, +0+ is returned. This method never
 raises an exception.

    "12345".to_i #=> 12345
    "99 red balloons".to_i #=> 99
    "0a".to_i #=> 0
    "0a".to_i(16) #=> 10
    "hello".to_i #=> 0
    "1100101".to_i(2) #=> 101
    "1100101".to_i(8) #=> 294977
    "1100101".to_i(10) #=> 1100101
    "1100101".to_i(16) #=> 17826049

```

irb(main):017:0\> “11111010”.to\_i(2)  
=\> 250  
irb(main):018:0\> “11111010”.to\_i(2).to\_s(16)  
=\> “fa”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![llinklater](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@llinklater](https://www.ruby-forum.com/u/llinklater)\
**Post date:** [October 6, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/finding-obscure-documentation/116438/7 "2007-10-06T16:01:36Z")

</div>

PeÃ±a, Botp wrote:

> ## ------------------------------------------------------------ Fixnum#to\_s fix.to\_s( base=10 ) -\> aString
> 
> ```
> Returns a string containing the representation of _fix_ radix
> _base_ (between 2 and 36).
> 
> ```
> 
> ## … ------------------------------------------------------------ String#to\_i str.to\_i(base=10) =\> integer
> 
> ```
> Returns the result of interpreting leading characters in _str_ as
> an integer base _base_ (2, 8, 10, or 16).
> 
> ```

I recognized that the documentation for “String#to\_i” is somehow  
“incomplete”, because it works like “Fixnum#to\_s” for a base between 2  
and 36:

> > > > > > > > > >

irb(main):001:0\> “gdfhjk”.to\_i(35)  
=\> 860522760  
irb(main):002:0\> “gdfhjk”.to\_i(36)  
=\> 990016400  
irb(main):003:0\> “gdfhjk”.to\_i(37)  
ArgumentError: illegal radix 37  
from (irb):3:in `to\_i’  
from (irb):3

> > > > > > > > > >

In details for each possible base:

> > > > > > > > > >

the\_number = 10101010101010

(2…36).each do |bas|  
if the\_number.to\_s(bas).to\_i(bas) == the\_number  
puts “base #{bas} works (#{the\_number.to\_s(bas)})”  
else  
puts “base #{bas} does not work”  
end  
end

> > > > > > > > > >

The result is:

> > > > > > > > > >

base 2 works (10010010111111010011000111100111001100010010)  
base 3 works (1022202122110202120001011121)  
base 4 works (2102333103013213030102)  
base 5 works (2310443332041213020)  
base 6 works (33252201234032454)  
base 7 works (2061526306133563)  
base 8 works (222772307471422)  
base 9 works (38678422501147)  
base 10 works (10101010101010)  
base 11 works (32448a5961a44)  
base 12 works (1171787a1872a)  
base 13 works (5836a100096a)  
base 14 works (26cc6a3aa16a)  
base 15 works (127b3cba6baa)  
base 16 works (92fd31e7312)  
base 17 works (50304bdeb05)  
base 18 works (2egahdbedfg)  
base 19 works (1c5e59eg4h9)  
base 20 works (jeb85d2caa)  
base 21 works (cf15igbbba)  
base 22 works (881bkk72d4)  
base 23 works (5dmfc043lj)  
base 24 works (3ji88medja)  
base 25 works (2g4nialbfa)  
base 26 works (1m9g6neiga)  
base 27 works (18khckf14g)  
base 28 works (qkh66m7aa)  
base 29 works (k5gfamce8)  
base 30 works (fbptcp6ka)  
base 31 works (bq4bh2she)  
base 32 works (95v9hssoi)  
base 33 works (760b79154)  
base 34 works (5manpab8m)  
base 35 works (4gyu22bfa)  
base 36 works (3kwc8m3gy)

> > > > > > > > > >

This means, that the documentation should be changed a little bit.

Wolfgang NÃ¡dasi-Donner
