How to pack an integer array to string?

I want to pack an array such as [1,2,3,4]
to “1234”, instead of \001\002\003\004, how to do that?

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

$ ri Array#join
------------------------------------------------------------- Array#join
array.join(sep=$,) -> str

 Returns a string created by converting each element of the array to
 a string, separated by _sep_.

    [ "a", "b", "c" ].join        #=> "abc"
    [ "a", "b", "c" ].join("-")   #=> "a-b-c"

$ irb
irb(main):001:0> [1,2,3,4].join
=> “1234”

HTH,

Felix

On Fri, Oct 12, 2007 at 01:42:15AM +0900, Liang He wrote:

I want to pack an array such as [1,2,3,4]
to “1234”, instead of \001\002\003\004, how to do that?

% irb

x = [1,2,3,4]
=> [1, 2, 3, 4]

x.join(’’)
=> “1234”

enjoy,

-jeremy

I just put eyes on ‘pack’, not aware that so many workarounds.
Thanks guys!

I just tried this in irb:

irb(main):112:0> [1,2,3,4].to_s
=> “1234”

From: John W. [mailto:[email protected]]

I just tried this in irb:

irb(main):112:0> [1,2,3,4].to_s

=> “1234”

careful w that.

C:\family\ruby>ruby -e “puts RUBY_VERSION;puts [1,2,3,4].to_s”
1.8.6
1234

C:\ruby1.9\bin>ruby -e “puts RUBY_VERSION;puts [1,2,3,4].to_s”
1.9.0
[1, 2, 3, 4]

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs