What's the opposite of chr() in Ruby?

In many languages there's a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord() is called asc().

Ruby has Integer#chr, which works great:

>> 65.chr
A

Fair enough. But how do you go the other way?

"A".each_byte do |byte|
   puts byte
end

prints:

65

and that's pretty close to what I want. But I'd really rather avoid a loop -- I'm looking for something short enough to be readable when declaring a const.

Answers


If String#ord didn't exist in 1.9, it does in 2.0:

"A".ord #=> 65

Need Your Help

Am I missing something about LINQ?

c# sql linq

I seem to be missing something about LINQ. To me, it looks like it's taking some of the elements of SQL that I like the least and moving them into the C# language and using them for other things.

Incrementing a local variable using JavaScript

javascript variables local counter increment

I am trying to increment a counter, but I would like to move the variable out of the global namespace and declare it locally. I'm not sure how to do this. Thanks for your help.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.