Can anyone tell me the computational logic of Unpack() method of string?

Hi,

I tried the below small fragments in my IRB :

$ ‘A’.unpack(‘b*’)
=> [“10000010”]

$ ‘A’.unpack(‘B*’)
=> [“01000001”]

$ “hello”.unpack(‘B*’)
=> [“0110100001100101011011000110110001101111”]

$ “hello”.unpack(‘C*’).map {|e| e.to_s 2}
=> [“1101000”, “1100101”, “1101100”, “1101100”, “1101111”]

But couldn’t understand the logic. So any help would be appreciated!

Thanks,

Read this carefully:

Given the sort of questions you’ve been posting, I have to ask. Are you
familiar with www.google.com ?

Joel P. wrote in post #1092065:

Read this carefully:
Class: String (Ruby 1.9.3)

Given the sort of questions you’ve been posting, I have to ask. Are you
familiar with www.google.com ?

Yes, I know. but one thing none of the docs I found have an
explanation,why the output is coming,what the logic behind it.Thus I
pasted here,hoping that people who worked long in this platform might
have practical experience on this,can give me the logic with
explanation. I have Google also but my bad i didn’t get any of such
where has some keen explanations.

Thanks

Because
uniq return new array
uniq! return new array or nil if no changes are made.
More info and examples you can get here

Regards,
Mateusz

rubyinfo wrote in post #1092071:

1.8.7 :001 > [].uniq!
=> nil
1.8.7 :002 > [].uniq
=> []

This was not my question, have you posted it with mine one by mistake or
what the reason is?

If by mistake please delete it!

1.8.7 :001 > [].uniq!
=> nil
1.8.7 :002 > [].uniq
=> []

1.9.3p194 :001 > [].uniq
=> []
1.9.3p194 :002 > [].uniq!
=> nil

Why ?

David

On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo [email protected] wrote:

Why ?

Why what?

Why are the results reversed? They’re not, you typed them in one
order the first time and the other the second.

Why does the bang version return nil? As the docs
(Class: Array (Ruby 1.9.3)) say,
“Returns nil if no changes are made (that is, no duplicates are
found).” Of course in [] there will be no dups.

Why IS there a difference? Generally, when there are both bang and
normal versions of a method, the bang version affects the object
passed in, while the normal version acts on (and returns) a duplicate.
In the bang case, you usually don’t care about the return value, and
are using it instead for the effect on the receiver.

Why do fools fall in love? Why do birds sing? Why is this night
different from all other nights? Why ask why, drink Bud Dry! :wink:

-Dave

Awesome explanation!

Dave A. wrote in post #1092074:

On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo [email protected] wrote:

Why ?

Why what?

What bad with you? why you posted your question with me? its not your
post! Create your new post then put the question. Just clean my post.
This is not your post, your question is conflicting with mine!

Open your eyes and see where you posted your question! BAD

Arup –

My apologies, I didn’t mean to steal your thread. I only read this list
by email so I didn’t realize it would hijack your thread.

Thanks for the informative responses.

Dave Heitzman

On Sat, Jan 12, 2013 at 1:27 PM, Arup R. [email protected]
wrote:

have practical experience on this,can give me the logic with
explanation. I have Google also but my bad i didn’t get any of such
where has some keen explanations.

Maybe you should read the code?

$ ‘A’.unpack(‘b*’)
=> [“10000010”]

$ ‘A’.unpack(‘B*’)
=> [“01000001”]

$ “hello”.unpack(‘B*’)
=> [“0110100001100101011011000110110001101111”]

$ “hello”.unpack(‘C*’).map {|e| e.to_s 2}
=> [“1101000”, “1100101”, “1101100”, “1101100”, “1101111”]

But couldn’t understand the logic. So any help would be appreciated!
Thanks,

Can anyone help me here to understand?

On Sun, Jan 13, 2013 at 3:36 AM, tamouse mailing lists
[email protected] wrote:

pasted here,hoping that people who worked long in this platform might
have practical experience on this,can give me the logic with
explanation. I have Google also but my bad i didn’t get any of such
where has some keen explanations.

Maybe you should read the code?

I’d start with the documentation and if something is not clear ask
/specific/ questions.

Cheers

robert

What is it you don’t understand exactly ?

Unpack decodes a string based on the format you specify and returns an
array of the decoded elements. The format specifier is made up of a
single character and may be followed by a number indicating how many
times to repeat the format specifier, or a ‘*’ to indicate all remaining
elements. In the examples you provided:

  1. ‘A’.unpack(‘b*’)

Will return a string representation of the binary value of ‘A’ in LSB
first order.

  1. ‘A’.unpack(‘B*’)

Will return a string representation of the binary value of ‘A’ in MSB
first order.

  1. “hello”.unpack(‘B*’)

This returns a string representation of the binary values ‘h’, ‘e’, ‘l’,
‘l’, ‘o’ in MSB first order

  1. “hello”.unpack(‘C*’).map { |e| e.to_s 2 }

Unpack creates an array of the integer values of ‘h’, ‘e’, ‘l’, ‘l’,
‘o’. This is then iterated over to create a new array by converting each
value to a string using Base2 or binary format.

Does that help ?

cheers,
Tony.

Tony H. wrote in post #1095012:

What is it you don’t understand exactly ?

Unpack decodes a string based on the format you specify and returns an
array of the decoded elements. The format specifier is made up of a
single character and may be followed by a number indicating how many
times to repeat the format specifier, or a ‘*’ to indicate all remaining
elements. In the examples you provided:

  1. ‘A’.unpack(‘b*’)

Will return a string representation of the binary value of ‘A’ in LSB
first order.

@Tony: Thank you very much!

Unpack decodes a string based on the format you specify and returns an

array of the decoded elements. The format specifier is made up of a
single character and may be followed by a number indicating how many
times to repeat the format specifier, or ‘*’ to indicate all remaining
elements. In the examples you provided:

Thanks for clearing this up for me. My thoughts were mixed up on this
one as well.

LSB standing for least significant bit - correct?

Yes, that’s right.

cheers,
Tony.