What is map(&:name) like?

Asked 2 years ago, Updated 2 years ago, 116 views

I saw this code in RailsCast and I don't know what map(&:name) is like (&:name).

def tag_names
  @tag_names || tags.map(&:name).join(' ')
end

parameter ruby syntax operator

2022-09-22 15:35

1 Answers

tags.map(&:name.to_proc).Same as short join(' ')

Can be written when foo is an object with the to_proc method, and

Handing over the factor with &foo is the same as handing over the result of calling foo.to_proc to the factor.

class Symbol
  def to_proc
    Proc.new do |obj, *args|
      obj.send self, *args
    end
  end
end


2022-09-22 15:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.