Unexpected Array#pack behaviour (bug?) 
Author Message
 Unexpected Array#pack behaviour (bug?)

Hi,

Do I miss something, or is ruby misbehaving here :

irb(main):072:0> ["a","b"].pack("m2")
"YQ==\n"
irb(main):073:0> ["a","b"].pack("mm")
"YQ==\nYg==\n"
irb(main):074:0> ["a","b"].pack("m*")
"YQ==\n"

Or is the ri - documentation misleading :
"If the count is an asterisk (``*''), all remaining array elements will
be converted."

Ruby 1.6.7 on Linux.

Han Holl



Thu, 11 Nov 2004 19:50:17 GMT  
 Unexpected Array#pack behaviour (bug?)

H> Or is the ri - documentation misleading :
H> "If the count is an asterisk (``*''), all remaining array elements will
H> be converted."

 ri

pigeon% irb
irb(main):001:0> ["abcdefghi", 2, 3, 4, 5, 6].pack("m*")
"YWJjZGVm\nZ2hp\n"
irb(main):002:0> ["abcdefghi", 2].pack("m*")
"YWJjZGVmZ2hp\n"
irb(main):003:0>
pigeon%

Guy Decoux



Thu, 11 Nov 2004 20:11:30 GMT  
 Unexpected Array#pack behaviour (bug?)

Quote:


>H> Or is the ri - documentation misleading :
>H> "If the count is an asterisk (``*''), all remaining array elements will
>H> be converted."

> ri

>pigeon% irb
>irb(main):001:0> ["abcdefghi", 2, 3, 4, 5, 6].pack("m*")
>"YWJjZGVm\nZ2hp\n"
>irb(main):002:0> ["abcdefghi", 2].pack("m*")
>"YWJjZGVmZ2hp\n"
>irb(main):003:0>
>pigeon%

Thanks, but I still don't get it:

irb(main):088:0> ["abcdefghi"].pack("m")
"YWJjZGVmZ2hp\n"

In your first example a "\n" gets inserted at a (seemingly) random
point. Is the idiom for converting an array of strings something
like:
anArray.pack("m" * anArray.size)

Han



Thu, 11 Nov 2004 20:37:28 GMT  
 Unexpected Array#pack behaviour (bug?)

H> In your first example a "\n" gets inserted at a (seemingly) random
H> point.

 For the format `m', the optional argument give the length of the string to
 encode (multiple of 3). If it's <= 2, ruby use the default value of 45

 ["abcdefghi", 2, 3, 4, 5, 6].pach("m*) <==> ["abcdefghi"].pack("m6")
 ["abcdefghi", 2].pach("m*) <==> ["abcdefghi"].pack("m2")

pigeon% irb
irb(main):001:0> ["abcdefghi"].pack("m6")
"YWJjZGVm\nZ2hp\n"
irb(main):002:0> ["abcdef"].pack("m")
"YWJjZGVm\n"
irb(main):003:0> ["ghi"].pack("m")
"Z2hp\n"
irb(main):004:0>
pigeon%

Guy Decoux



Thu, 11 Nov 2004 20:47:52 GMT  
 Unexpected Array#pack behaviour (bug?)

Quote:


>H> In your first example a "\n" gets inserted at a (seemingly) random
>H> point.

> For the format `m', the optional argument give the length of the string to
> encode (multiple of 3). If it's <= 2, ruby use the default value of 45

> ["abcdefghi", 2, 3, 4, 5, 6].pach("m*) <==> ["abcdefghi"].pack("m6")
> ["abcdefghi", 2].pach("m*) <==> ["abcdefghi"].pack("m2")

Thanks a lot, Guy. I would never have guessed.
This certainly isn't to the principle of my least surprise <g>

Han



Thu, 11 Nov 2004 20:58:15 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Unexpected listbox behaviour (bug?)

2. Unexpected combobox IWidget grab-behaviour (bug?)

3. pack.n and pack-old.n bug

4. unexpected behaviour of }

5. Unexpected IF/AND behaviour

6. Unexpected behaviour (for a beginner)

7. Unexpected behaviour with CancelAutoInc - HELP!

8. Unexpected LOCAL behaviour

9. Unexpected behaviour of IFNDEF - workaround sought

10. unexpected behaviour in class variables

11. Unexpected behaviour of event bindings in text widget

12. dynamic switch statement, unexpected behaviour

 

 
Powered by phpBB® Forum Software