Convert an Character to the ASCII Character Code

I have Ruby 1.9.1p129 and I want to convert an ASCII character to an
Integer and a Float.

While reading the following web page, I found conflicts in the code
usage:

‘Convert an Character to the ASCII Character Code’

Integer (?e)
=> 101

(then further down)

Float (?e)
=> 101.0

I received errors as per the attached screen snapshot.

I have found out that it is because I have version 1.9 and the above was
only workable in version 1.8.

I would still like to know how to get the same results using my current
version.

Thanks.

On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:

I would still like to know how to get the same results using my current
version.

How about this:

“e”.getbyte(0)

and:

“e”.getbyte(0).to_f

Aaron P. wrote:

On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:

I would still like to know how to get the same results using my current
version.

How about this:

“e”.getbyte(0)

and:

“e”.getbyte(0).to_f

That is perfect, thanks a lot!

Aaron P. wrote:

On Mon, Jul 06, 2009 at 07:32:38AM +0900, John Gregory wrote:

I would still like to know how to get the same results using my current
version.

How about this:

“e”.getbyte(0)

and:

“e”.getbyte(0).to_f

Or:

irb(main):001:0> “e”.ord
=> 101

This is a bit different, as it works in terms of codepoints (characters)
rather than bytes. This won’t make a difference if you are using
US-ASCII only.

irb(main):003:0> “ê”.encoding
=> #Encoding:UTF-8
irb(main):004:0> “ê”.ord
=> 234
irb(main):005:0> “ê”.bytesize
=> 2
irb(main):006:0> “ê”.getbyte(0)
=> 195
irb(main):007:0> “ê”.getbyte(1)
=> 170