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