In Ruby code, often it is observed that coder uses a short hand form of using an expression like array.map(&:method_name) instead of array.map { |element| element.method_name }. How this trick actually works?



In Ruby code, often it is observed that coder uses a short hand form of using an expression like arr..

Answer / Dinesh Chandra Tripathi

This is called the symbolization shortcut in Ruby. It allows you to pass the method name as a symbol instead of a block. When using &, it dereferences the Proc object from the Symbol and applies it to each element in the array.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Ruby Interview Questions

Explain about ruby names?

1 Answers  


What are the key features of ruby?

1 Answers  


Explain ruby strings.

1 Answers  


How symbol is different from variables?

1 Answers  


Tell us how would you implement hash in ruby internally?

1 Answers  


Tell me how you define instance variable, global variable and class variable in ruby?

1 Answers  


Is ruby a dying language?

1 Answers  


Are ruby strings are mutable?

1 Answers  


Do you know about dig, float and max?

1 Answers  


How do you remove nil values in array using ruby?

1 Answers  


Explain the use of retry statement in ruby?

1 Answers  


what is the purpose of the rakefile available in the demo directory in ruby?

1 Answers  


Categories