Chars in Ruby

Discussion in 'Ruby' started by Georgy, Jun 5, 2004.

  1. Georgy

    Georgy Guest

    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.@')
     
    Georgy, Jun 5, 2004
    #1
    1. Advertising

  2. il Sat, 05 Jun 2004 10:40:45 GMT, "Georgy" <> 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()
     
    gabriele renzi, Jun 5, 2004
    #2
    1. Advertising

  3. Georgy

    Georgy Guest

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

    "gabriele renzi" <> wrote in message news:...
    |
    | use pack() and unpack()
     
    Georgy, Jun 5, 2004
    #3
  4. "Georgy" <> 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
     
    Shashank Date, Jun 5, 2004
    #4
  5. Georgy

    Georgy Guest

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

    "Shashank Date" <> wrote in message news:...
    |
    | "Georgy" <> 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.@')
     
    Georgy, Jun 5, 2004
    #5
  6. "Georgy" <> schrieb im Newsbeitrag
    news:Nqhwc.15070$...
    > 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
     
    Robert Klemme, Jun 6, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,295
    Tim Rentsch
    Sep 23, 2005
  2. Hongyu
    Replies:
    9
    Views:
    916
    James Kanze
    Aug 8, 2008
  3. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    233
    Dan Rogers
    Nov 16, 2004
  4. anne001
    Replies:
    1
    Views:
    476
  5. John
    Replies:
    3
    Views:
    319
    Matt Neuburg
    Jul 30, 2009
Loading...

Share This Page