← Back to Gists

Symbol to proc

📝 Ruby
jamesgarcia426
jamesgarcia426 · Level 12 ·

This snippet converts a symbol into a proc that calls the matching method on each argument, letting you write concise enumerable operations like &:upcase.

Ruby
# Symbol#toproc allows using &:method as a shorthand
strings = %w[hello world ruby]
# Using &:upcase
puts strings.map(&:upcase).inspect # Manual equivalent: :upcase.toproc creates a Proc
procobj = :upcase.toproc
puts strings.map(&procobj).inspect

Comments

No comments yet. Start the discussion.