finding obscure documentation

  • Thread starter Lloyd Linklater
  • Start date
L

Lloyd Linklater

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.
 
K

Konrad Meyer

--nextPart20203525.nMOOVUyGsE
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Quoth Lloyd Linklater:
I have learned that you can turn a number to octal (or whatever) this
way:
=20
p 123.to_s(8).to_i
=3D> 173
=20
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.
=20
My question is, where is this and things like it documented? I have
searched several books to no avail.

"0x123".to_i(16) # =3D> 291

Your method of "turn[ing] a number to octal" turns it to octal in a string,=
=20
then converts it back to an integer. You really want to leave off the .to_i=
=20
bit.

HTH,
=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart20203525.nMOOVUyGsE
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHBkPPCHB0oCiR2cwRArgNAJ4iXDMMrK7lH/HbPclHrHLyM4ZCUwCeN+Pz
l0Y0H1p/rvlT477dVFA2luI=
=ZsPG
-----END PGP SIGNATURE-----

--nextPart20203525.nMOOVUyGsE--
 
R

Rob Biedenharn

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.

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
S

Simon Krahnke

* Lloyd Linklater said:
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
 
L

Lloyd Linklater

Rob said:
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!
 
W

Wolfgang Nádasi-Donner

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,344
Members
48,019
Latest member
Migration_Expert

Latest Threads

Top