Ruby Symbol#to_proc — Don’t Forget It!

This is your code:

puts %w{A B}.collect{|e| e.downcase}.inspect

This is my cooler code:

puts %w{A B}.collect( &:downcase).inspect

This is how the Ruby Extension Project makes it work:

class Symbol
  def to_proc
    proc { |obj, *args| obj.send(self, *args) }
  end
end

This is the explanation.

This is the love (see day 38).

Don’t ever forget this.

About these ads
This entry was posted in Ruby on Rails. Bookmark the permalink.

One Response to Ruby Symbol#to_proc — Don’t Forget It!

  1. Pingback: Symbol#to_proc (&:) « Scriptrunner's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s