Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Please explain that

Reply
Thread Tools

Please explain that

 
 
Jan Pilz
Guest
Posts: n/a
 
      09-10-2008
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 ?

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 3:24 PM, Jan Pilz <> 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.

 
Reply With Quote
 
 
 
 
Rimantas Liubertas
Guest
Posts: n/a
 
      09-10-2008
> 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
--
http://rimantas.com/

 
Reply With Quote
 
Gregory Brown
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 9:24 AM, Jan Pilz <> 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(

=> "71"
>> 57.to_s(

=> "71"


--
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com

 
Reply With Quote
 
Jano Svitok
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 15:24, Jan Pilz <> 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.

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 3:33 PM, Gregory Brown
<> 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.

 
Reply With Quote
 
Gregory Brown
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 9:47 AM, Xavier Noria <> wrote:
> On Wed, Sep 10, 2008 at 3:33 PM, Gregory Brown
> <> 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

--
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com

 
Reply With Quote
 
Gregory Seidman
Guest
Posts: n/a
 
      09-10-2008
On Wed, Sep 10, 2008 at 10:24:18PM +0900, Jan Pilz 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Please explain this "Why's" example please Kaye Ng Ruby 8 06-08-2010 09:13 AM
Explain this a little better please. fwells11@hotmail.com Cisco 3 07-31-2005 06:49 AM
Someone please explain to me ... =?Utf-8?B?bXJzLmdyYW50?= Wireless Networking 6 11-25-2004 10:08 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57