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,

···

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

Read this carefully:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-unpack

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

···

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

$ '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?

···

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

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?

···

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

Joel Pearson 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

···

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

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.

2. 'A'.unpack('B*')

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

3. "hello".unpack('B*')

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

4. "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.

···

On 3 Feb 2013, at 11:58, Arup Rakshit <lists@ruby-forum.com> wrote:

$ '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?

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

Yes, that's right.

cheers,
Tony.

···

On 6 Feb 2013, at 22:41, Ronnie Sims <lists@ruby-forum.com> wrote:

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?

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

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

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

Why ?

David

Maybe you should read the code?

···

On Sat, Jan 12, 2013 at 1:27 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Joel Pearson wrote in post #1092065:

Read this carefully:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-unpack

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.

Tony Headford 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!

···

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

Because
  uniq return new array
  uniq! return new array or nil if no changes are made.
More info and examples you can get here
http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-uniq
Regards,
Mateusz

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

···

On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo <rubyinfo@aptifuge.com> wrote:

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

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

Why ?

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

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!

···

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

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

Cheers

robert

···

On Sun, Jan 13, 2013 at 3:36 AM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Sat, Jan 12, 2013 at 1:27 PM, Arup Rakshit <lists@ruby-forum.com> wrote:

Joel Pearson 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.

Maybe you should read the code?

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Dave Aronson wrote in post #1092074:

···

On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo <rubyinfo@aptifuge.com> 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

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

Awesome explanation!

···

On Sunday, 13 January 2013 г. at 3:14, Dave Aronson wrote:

On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo <rubyinfo@aptifuge.com (mailto:rubyinfo@aptifuge.com)> wrote:

> 1.8.7 :001 > .uniq!
> => nil
> 1.8.7 :002 > .uniq
> =>
>
> 1.9.3p194 :001 > .uniq
> =>
> 1.9.3p194 :002 > .uniq!
> => nil
>
>
> 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

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

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