SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is this syntax: &:day

    Hello,

    I've been using Ruby and Rails for about 4 months now and although not often, I've seen this syntax from time to time. What does it mean? For example, in Rails's latest blog post:

    Code:
    latest_transcripts.group_by(&:day).each do |day, transcripts| 
      
     p "#{day} -> #{transcripts.map(&:class) * ', '}"
      end
      "2006-03-01 -> Transcript" 
      "2006-02-28 -> Transcript" 
      "2006-02-27 -> Transcript, Transcript" 
      "2006-02-26 -> Transcript, Transcript" 
    See the &:day and &:class? What are these?

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is "to_proc" defined for the Symbol class.

    Code:
    class Symbol
       def to_proc
    		proc { |obj, *args| obj.send(self, *args) }
       end
    end
    
    p ["abc", "def"].map(&:reverse)
    p [1, 2, 3].inject(&:+)
    See this thread
    http://groups.google.com/group/comp....669444207f718b

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow. Very cool! Ruby never ceases to amaze me


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •