0

I'm new to ruby and i want to print the last element in a sub array from a 2d array. I've tried using an each iterator but it gave me an error: "no implicit conversion of Array into Integer (TypeError)"

pairs = [['name', 'sam'], ['age', '56'], ['height', '179']]

pairs.each{ |x| puts pairs[x][1] } 

Preferred output:
sam
56
179

Any suggestions will be appreciated! Thanks.

1
  • Within the each block, x refers to a single sub-array at a time, e.g. for the first iteration you can think of x = ['name', 'sam']. Does that help? Commented Jun 17, 2021 at 6:45

1 Answer 1

2

You are almost there pairs.each { |x| puts x[1] }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.