Finding obscure documentation

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.

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,

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
[email protected]

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!

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

From: [email protected]

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs