Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Chars in Ruby

Reply
Thread Tools

Chars in Ruby

 
 
Georgy
Guest
Posts: n/a
 
      06-05-2004
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

--
Georgy Pruss
E#Mail: 'naabbcaDaddaLryDwksvKmyw'.tr('a-zA-Z','0-9a-z.@')


 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      06-05-2004
il Sat, 05 Jun 2004 10:40:45 GMT, "Georgy" <(E-Mail Removed)> ha scritto::

>Hi,


>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()
 
Reply With Quote
 
 
 
 
Georgy
Guest
Posts: n/a
 
      06-05-2004
Ah! Thanks! I suspected there must be something better
G-:

"gabriele renzi" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
|
| use pack() and unpack()


 
Reply With Quote
 
Shashank Date
Guest
Posts: n/a
 
      06-05-2004

"Georgy" <(E-Mail Removed)> wrote in message
> 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


 
Reply With Quote
 
Georgy
Guest
Posts: n/a
 
      06-05-2004
Thank you! That's what I wanted. I didn't think split would treat '' so specially.
G-:

"Shashank Date" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
|
| "Georgy" <(E-Mail Removed)> wrote in message
| > 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
|

--
Georgy Pruss
E#Mail: 'naabbcaDaddaLryDwksvKmyw'.tr('a-zA-Z','0-9a-z.@')


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      06-06-2004

"Georgy" <(E-Mail Removed)> schrieb im Newsbeitrag
news:Nqhwc.15070$(E-Mail Removed) om...
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby 1.8.6 puts outputs \000 chars John Ruby 3 07-30-2009 04:34 PM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments