Ruby: rules for implicit hashes

Posted by flyer on Stack Overflow See other posts from Stack Overflow or by flyer
Published on 2013-11-08T23:23:08Z Indexed on 2013/11/09 3:55 UTC
Read the original article Hit count: 180

Filed under:
|
|
|

Why second output shows me only one element of Array? Is it still Array or Hash already?

def printArray(arr)
    arr.each { | j |
        k, v = j.first
        printf("%s %s %s \n", k, v, j)
    }
end

print "Array 1\n"
printArray( [
                {kk: { 'k1' => 'v1' }},
                {kk: { 'k2' => 'v2' }},
                {kk: { 'k3' => 'v3' }},
            ])


print "Array 2\n"
printArray( [
                kk: { 'k1' => 'v1' },
                kk: { 'k2' => 'v2' },
                kk: { 'k3' => 'v3' },
            ])

exit

# Output:
#
# Array 1
# kk {"k1"=>"v1"} {:kk=>{"k1"=>"v1"}} 
# kk {"k2"=>"v2"} {:kk=>{"k2"=>"v2"}} 
# kk {"k3"=>"v3"} {:kk=>{"k3"=>"v3"}} 
# Array 2
# kk {"k3"=>"v3"} {:kk=>{"k3"=>"v3"}}

© Stack Overflow or respective owner

Related posts about ruby

Related posts about arrays