0

So, for example you have a document: Ruby.txt, the contents of which are

RuBYrUbYRuByByurBRyuRuby

You for some reason need to convert the document's contents into ASCII code without printing it onto your screen (change the document's contents). How do you do that?

P.S. Sorry, no code to debug

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Delta
  • 55
  • 1
  • 7

2 Answers2

0
"RuBYrUbYRuByByurBRyuRuby".unpack("c*")
 => [82, 117, 66, 89, 114, 85, 98, 89, 82, 117, 66, 121, 66, 121, 117, 114, 66, 82, 121, 117, 82, 117, 98, 121]
Hooopo
  • 1,380
  • 10
  • 16
0

It can depend on your ruby version...
but this should work on both 1.8 and 1.9

1.9.3p194 :062 > "string".each_byte do |c|
1.9.3p194 :063 >     puts c.ord
1.9.3p194 :064?>   end
115
116
114
105
110
103
 => "string" 
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497