[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

to_s(base=2) in 1.8 only?

ts

1/16/2009 9:21:00 AM

f.each_byte {|x| print x.to_s(base=2).rjust(8,'0')

the above will fail in 1.6.4

wanna ask if to_s(base=2) is only available in 1.8?

my intention is to get each byte and output it as 10101010 format. Is
there any way that i can do that in 1.6.4?

thank you.
2 Answers

Robert Dober

1/16/2009 9:32:00 AM

0

On Fri, Jan 16, 2009 at 10:23 AM, ts <thaisiang@gmail.com> wrote:
> f.each_byte {|x| print x.to_s(base=2).rjust(8,'0')
>
> the above will fail in 1.6.4
>
> wanna ask if to_s(base=2) is only available in 1.8?
>
> my intention is to get each byte and output it as 10101010 format. Is
> there any way that i can do that in 1.6.4?
>
> thank you.
>
>
I cannot test with 1.6.4 but probably the following works

"%b" % x

Pas d'quoi ;)
R.



--
It is change, continuing change, inevitable change, that is the
dominant factor in society today. No sensible decision can be made any
longer without taking into account not only the world as it is, but
the world as it will be ... ~ Isaac Asimov

Ken Bloom

1/16/2009 3:02:00 PM

0

On Fri, 16 Jan 2009 01:21:15 -0800, ts wrote:

> f.each_byte {|x| print x.to_s(base=2).rjust(8,'0')

Can't speak to the Ruby 1.6 issue, but the base= doesn't add anything to
your code (it just assigned 2 to a local variable). Just call x.to_s(2)

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...