Array methods creating confusions as per their functionalities

Can any one just elaborate how the below works in Ruby, by definition
they are not quite clear. Hope a small code snippet may help to
understand them at their core,how they works?

array.zip(arg, …)

array.transpose

array.rassoc(key)

array.pack(aTemplateString)

array.frozen?

array.flatten

array.hash

Thanks in advance!

Hello,

I don’t want to be rude or anything, but haven’t you read the
documentation over at Class: Array (Ruby 1.9.3) ?
All the methods you asked us to explain are documented there. If I
remember correctly, you also asked for the difference for map and map!
not too long ago. The solution also was written in the documentation, as
was pointed out.

In general, I always found a google search with “Ruby [classname]”
giving me all the info I wanted. It’s also much quicker than asking in
the list. :wink:

Kind regards,
Calvin

Try reading zip (Array) - APIdock etc.

Calvin B. wrote in post #1092020:

Hello,

I agree with your point, as I am a newbie so not that much confident on
the methods how are they defined in the docs.I tried to understand a bit
by test code,nothing in my HEAD! :frowning: here is a small code for Pack()
method of Array class instance.

irb(main):001:0> n = [ 65, 66, 67 ]
=> [65, 66, 67]
irb(main):002:0> n.pack(“CCC”)
=> “ABC”
irb(main):003:0> n.pack(“ccc”)
=> “ABC”
irb(main):004:0> n.pack(“sss”)
=> “A\x00B\x00C\x00”
irb(main):005:0> n.pack(“SSS”)
=> “A\x00B\x00C\x00”
irb(main):006:0> n.pack(“qqq”)
=>
“A\x00\x00\x00\x00\x00\x00\x00B\x00\x00\x00\x00\x00\x00\x00C\x00\x00\x00\x00
x00\x00\x00”
irb(main):007:0> n.pack(“QQQ”)
=>
“A\x00\x00\x00\x00\x00\x00\x00B\x00\x00\x00\x00\x00\x00\x00C\x00\x00\x00\x00
x00\x00\x00”
irb(main):008:0>

Now here you can see that I used directives as c and C;q and Q .
But looking at the console output i didn’t get any differences. But I
know there should be,so need your help to pin point me,as you are
experienced! I always obey your guide,directions n all.

Arup R. wrote in post #1092023:

Alex van der Ruchei wrote in post #1092021:

Try reading zip (Array) - APIdock etc.

Wow nice docs you have provided! its too handy to understand as it
explains everything with small codes.

let me look into the docs you just provided.

Thanks for your time!

to understand the ZIP method i used the below code,but getting an
error.I might overlooked the bad area.can you help?

irb(main):008:0> a = [ 4, 5, 6 ]
=> [4, 5, 6]
irb(main):009:0> b = [ 7, 8, 9 ]
=> [7, 8, 9]
irb(main):010:0> [1,2].zip(a,b)
=> [[1, 4, 7], [2, 5, 8]]
irb(main):011:0> a.zip([1,2,]){|i|i>4}
NoMethodError: undefined method >' for [4, 1]:Array from (irb):11:in block in irb_binding’
from (irb):11:in zip' from (irb):11 from C:/Ruby193/bin/irb:12:in
irb(main):012:0> a.zip([1,2,]){|i|i>4}
NoMethodError: undefined method >' for [4, 1]:Array from (irb):12:in block in irb_binding’
from (irb):12:in zip' from (irb):12 from C:/Ruby193/bin/irb:12:in
irb(main):013:0> a.zip([1,2,]){|i|(i>4)}
NoMethodError: undefined method >' for [4, 1]:Array from (irb):13:in block in irb_binding’
from (irb):13:in zip' from (irb):13 from C:/Ruby193/bin/irb:12:in
irb(main):014:0> a.zip([1,2,]){|i|i>4}
NoMethodError: undefined method >' for [4, 1]:Array from (irb):14:in block in irb_binding’
from (irb):14:in zip' from (irb):14 from C:/Ruby193/bin/irb:12:in
irb(main):015:0>

Thanks!

Arup R. wrote in post #1092018:

Can any one just elaborate how the below works in Ruby, by definition
they are not quite clear. Hope a small code snippet may help to
understand them at their core,how they works?

array.frozen?

My confusion or knowledge gap here is what the frozen means in Ruby
platform with the array? How an array can be frozen. The document said
only what the functions does in reality. which is “Return true if this
array is frozen (or temporarily frozen while being sorted).” . But this
is for me not enough to understand why Ruby brought the “frozen”
concept. How and what scenarios made an array as frozen array in ruby?
here is the pain. I hope that anyone of this forum will help me out to
clear the logic within me.

Thanks!

You can “freeze” most mutable objects (by calling #freeze) to make them
immutable.

You can still e.g. access elements of a frozen array, but trying to
add/remove/modify them will cause an exception.

Alex van der Ruchei wrote in post #1092021:

Try reading zip (Array) - APIdock etc.

Wow nice docs you have provided! its too handy to understand as it
explains everything with small codes.

let me look into the docs you just provided.

Thanks for your time!

Bartosz Dziewoński wrote in post #1092029:

You can “freeze” most mutable objects (by calling #freeze) to make them
immutable.

You can still e.g. access elements of a frozen array, but trying to
add/remove/modify them will cause an exception.

any small code, on your sentences,if you give,it would be better for
understand with scenarios!

On Saturday 12 January 2013 Arup R. wrote

only what the functions does in reality. which is "Return true if this
Posted via http://www.ruby-forum.com/.
A frozen object is an object which can’t be modified. You freeze an
object
using its freeze method. To see an example, run the following code:

a=[1,2,3]
a.delete_at 1
p a
b=[1,2,3]
b.freeze
b.delete_at 1

You’ll see that, while a.delete_at 1 changes the array, b.delete_at 1
raises
an exception, because b had been frozen and therefore it can’t be
modified.

According to the questions you’re posting, it seems to me you haven’t
read a
comprehensive ruby book. Have you tried the online version of
Programming Ruby
(Programming Ruby: The Pragmatic Programmer's Guide)? It free and, even if
written
for an old version of ruby, most of it is still accurate.

I hope this helps

Stefano