Chars in Ruby

G

Georgy

Hi,

I've been playing with characters (OK, there's no such type in
Ruby, it's rather like characters in C - just plain integer numbers).

String of length 1 to a char: str1[0] --> chr_int
Char to a String of length 1: chr_int.chr --> str1

Get a char from a string: str[n] --> chr_int # or nil
Put a char into a string: str[n] = chr_int

So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.

Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
String to Array: "abc" ????????????? --> [97,98,99]

I've found that this function can do it:

def s2a(s); r=[]; s.each_byte {|b| r<<b}; r; end
s2a("abc") --> [97,98,99]

But can it be an expression w/o a function or with a shorter
"more elegant" function, w/o accumulator r?

Regards,
Georgy
 
G

gabriele renzi

So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.

Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
String to Array: "abc" ????????????? --> [97,98,99]

I've found that this function can do it:

def s2a(s); r=[]; s.each_byte {|b| r<<b}; r; end
s2a("abc") --> [97,98,99]

But can it be an expression w/o a function or with a shorter
"more elegant" function, w/o accumulator r?

use pack() and unpack()
 
G

Georgy

Ah! Thanks! I suspected there must be something better :)
G-:

|
| use pack() and unpack()
 
S

Shashank Date

Georgy said:
Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
String to Array: "abc" ????????????? --> [97,98,99]

How about: "abc".split('').collect{|e| e[0]}

Just applied the inverse transforms of what you did to get Array
to String ...and of course, as GR mentioned, you can pack/unpack.
E#Mail: 'naabbcaDaddaLryDwksvKmyw'.tr('a-zA-Z','0-9a-z.@')

-- shanko
 
G

Georgy

Thank you! That's what I wanted. I didn't think split would treat '' so specially.
G-:

|
| > Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"
| > String to Array: "abc" ????????????? --> [97,98,99]
|
| How about: "abc".split('').collect{|e| e[0]}
|
| Just applied the inverse transforms of what you did to get Array
| to String ...and of course, as GR mentioned, you can pack/unpack.
|
| -- shanko
|
 
R

Robert Klemme

Georgy said:
Hi,

I've been playing with characters (OK, there's no such type in
Ruby, it's rather like characters in C - just plain integer numbers).

String of length 1 to a char: str1[0] --> chr_int
Char to a String of length 1: chr_int.chr --> str1

Get a char from a string: str[n] --> chr_int # or nil
Put a char into a string: str[n] = chr_int

So we can think of a "char" as a single element of a string.
Now, having that an integer is a single element of an array,
we'd like to have functions/expressions for converting arrays
of integers and strings back and forth.

Array to String: [97,98,99].map{|e|e.chr}.join --> "abc"

This is more efficient:

[97,98,99].inject(""){|str,ch|str<<ch}

But of course the pack / unpack pair is the most appropriate way to do it as
others told already.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top