Chaining & to_proc on symbol

后端 未结 5 741
深忆病人
深忆病人 2020-12-09 22:26

It\'s well known to Rubyist & will call to_proc on a symbol, so

[:a, :b, :c].map(&:to_s)

is equivalent to

5条回答
  •  时光说笑
    2020-12-09 23:01

    There is no way to chain using the symbol to proc.

    However, you could monkey patch a method to the class you are mapping over that will do both, then call that.

    class Symbol
      def to_upcase_str
        self.to_s.upcase
      end
    end
    
    [:a, :b, :c].map(&:to_upcase_str)
    

提交回复
热议问题