From: Duong Quang Ha Date: 2012-05-23T21:40:13+09:00 Subject: Re: How to replace a letter with a corresponding number --047d7b2e446842446404c0b36f4b Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, I'm sorry, the last command must be: result =3D s.split('').collect { |x| alpha_table[x] } On Wed, May 23, 2012 at 7:36 PM, Duong Quang Ha wro= te: > Hello, > > You can do this task in steps below: > 1. Create a hash contains character as key and its corresponding number a= s > value. > 2. Use String#split to split the String into an array. > 3. Use Array#collect to convert characters to corresponding number. > > Example: > > # Create hash contains alphabetical table > alpha_table =3D {} > (('A'..'Z').zip(1..26)).each { |x| alpha_table[x[0]] =3D x[1] } > > s =3D "RUBYC" # String to convert > result =3D s.to_a.collect { |x| alpha_table[x] } > > > On Wed, May 23, 2012 at 7:22 PM, Rich McMullen wrot= e: > >> I am working through the Ruby Quiz website and I am already stuck on >> number 1 - Solitaire Cypher. >> >> I have a string, let's say "RUBYC ODEIS THEBE STXXX". I need to convert >> each letter to its corresponding number (i.e., A=3D1, B=3D2, etc.). >> >> I had hoped I could use gsub with a regexp like this: >> >> coded_text =3D text.gsub(/[A-Z/,[1-26]) >> >> but that doesn't work. Any ideas? >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > Regards, > > -- > Duong Quang Ha (H=C3=A0 Quang D=C6=B0=C6=A1ng in Vietnamese) > blog.haqduong.com > > --=20 Duong Quang Ha (H=C3=A0 Quang D=C6=B0=C6=A1ng in Vietnamese) blog.haqduong.com --047d7b2e446842446404c0b36f4b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi,

I'm sorry, the last command must be:
<= br>
result =3D s.split('').collect { |x| alpha_table[x] }=

On Wed, May 23, 2012 at 7:36 PM, Duong Q= uang Ha <quangduongha@gmail.com> wrote:
Hello,

You can do this ta= sk in steps below:
1. Create a hash contains character as key and= its corresponding number as value.
2. Use String#split to split the String into an array.
3. Use Array#collect to convert characters to=C2=A0corresponding numbe= r.

Example:

# Create hash= contains alphabetical table
alpha_table =3D {}
(('= A'..'Z').zip(1..26)).each { |x| alpha_table[x[0]] =3D x[1] }

s =3D=C2=A0"RUBYC" # String to convert
<= div>result =3D s.to_a.collect { |x| alpha_table[x] }


On Wed, May 23, 2012 at 7:22 = PM, Rich McMullen <lists@ruby-forum.com> wrote:
I am working through the Ruby Quiz website a= nd I am already stuck on
number 1 - Solitaire Cypher.

I have a string, let's say "RUBYC ODEIS THEBE STXXX". =C2=A0I= need to convert
each letter to its corresponding number (i.e., A=3D1, B=3D2, etc.).

I had hoped I could use gsub with a regexp like this:

coded_text =3D text.gsub(/[A-Z/,[1-26])

but that doesn't work. =C2=A0Any ideas?

--
Posted via http://= www.ruby-forum.com/.



Regards,

--
Du= ong Quang Ha (H=C3=A0 Quang D=C6=B0=C6=A1ng in Vietnamese)



--
Duong Quang = Ha (H=C3=A0 Quang D=C6=B0=C6=A1ng in Vietnamese)
blog.haqduong.com

--047d7b2e446842446404c0b36f4b--