0

I accidentally wrote pluck(&:id) instead of pluck(:id) and I got some unusual results and I'm curious what is actually happening. What came back looks like an array of all the values (no keys) from the object I was plucking from.

The results looked like this:

=> [[5874, "Vancouver", Wed, 01 Mar 2023, Wed, 01 Mar 2023, nil, nil, nil, nil, nil, nil, nil, nil, true, false, false, 212388, 212388, nil, false, nil, false, nil, false, "Vancouver, BC", true, false, "23VBCCIOIC5874", false, nil, nil]]

I then noticed that I could put anything after the & and it didn't matter. Such as pluck(&:ham_sandwich) would give the same results.

Dan
  • 1,238
  • 2
  • 17
  • 32
  • 2
    Does this answer your question? [what is the functionality of "&: " operator in ruby?](https://stackoverflow.com/a/9429972/895789) – Alter Lagos May 12 '22 at 23:57
  • 1
    `pluck` method checks if the passed argument matches any column name or not, if yes then it returns the value of that column, and if no it returns the value of all columns. `:id` matches the `id` column and `&:id` matches none so that is why you are getting this output. Try calling only `pluck` without any arguments it would return all the columns. So there is no role of `&` here. – Deepesh May 13 '22 at 07:17

0 Answers0