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,
on 2013-01-12 18:21
on 2013-01-12 20:12
Read this carefully: http://www.ruby-doc.org/core-1.9.3/String.html#met... Given the sort of questions you've been posting, I have to ask. Are you familiar with www.google.com ?
on 2013-01-12 20:27
Joel Pearson wrote in post #1092065: > Read this carefully: > http://www.ruby-doc.org/core-1.9.3/String.html#met... > > 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
on 2013-01-12 21:06
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 2013-01-12 21:14
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
on 2013-01-12 21:15
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!
on 2013-01-12 21:15
On Sat, Jan 12, 2013 at 3:03 PM, rubyinfo <rubyinfo@aptifuge.com> 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 (http://www.ruby-doc.org/core-1.9.3/Array.html#meth...) 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 2013-01-12 21:23
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
on 2013-01-13 02:49
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 2013-01-13 03:38
On Sat, Jan 12, 2013 at 1:27 PM, Arup Rakshit <lists@ruby-forum.com> 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?
on 2013-01-14 08:49
On Sun, Jan 13, 2013 at 3:36 AM, tamouse mailing lists <tamouse.lists@gmail.com> 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
on 2013-02-03 12:58
> $ '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 2013-02-03 17:05
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 2013-02-03 17:10
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!
on 2013-02-06 23:41
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?
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.