for this array: [{p:10, n:"m", size: {s: 7, m: 2}} how to display only the 'size'? in ruby

To display only the 'size' from the given array in Ruby, you can use the following code:

main.rb
array = [{p: 10, n: "m", size: {s: 7, m: 2}}]
size = array[0][:size]
puts size.inspect
87 chars
4 lines

Output:

main.rb
{:s=>7, :m=>2}
15 chars
2 lines

In the above code, we first define the array with the given values. Then, we access the first element of the array using array[0] and further access the 'size' key using [:size]. Finally, we print the 'size' using puts and inspect to display its contents.

Note that the output is in the form of a nested hash, where each key represents the size label (s, m) and its value represents the corresponding size value.

related categories

gistlibby LogSnag