Robert Grezan
Robert Grezan

Reputation: 1274

How to write "string".encode("us-ascii") in ruby 1.8.7?

I'm trying to use the "gmail_xoauth" gem and, unfortunately, the gem uses encode("us-ascii") for strings, which is available only for Ruby 1.9.3.

I'm not familiar with encode in Ruby 1.9.3 so I wonder what is "string".encode("us-ascii") and how do I write it for 1.8.7?

Upvotes: 3

Views: 5682

Answers (1)

joelparkerhenderson
joelparkerhenderson

Reputation: 35443

The string.encode("us-ascii") method converts all the characters in the string to United States ASCII 7-bit values.

US-ASCII is essentially plain text with 128 characters total. That encoding was common on United States computers in the 1970s-1990s.

The reason you're seeing it now is probably because you're using email. The email protocol requires US-ASCII encoding for strings.

Ruby 1.8.7 doesn't have string encoding methods built-in because Ruby 1.8.7 stores strings as bytes, not encoded characters.

To convert in Ruby 1.8.7, see the Iconv library:

http://ruby-doc.org/stdlib-1.8.7/libdoc/iconv/rdoc/Iconv.html

Also see the conversion iconv code sample in this answer:

String.force_encoding() in Ruby 1.8.7 (or Rails 2.x)

Upvotes: 3

Related Questions