Skip to main content

Clarification needed for simple_calendar sort_by method

General • Asked by sid d

Hi Chris,

I've seen simple_calendar ruby gem and i stuck at sort_by method (calendar.rb)

def events_for_date(current_date)
     if events.any? && events.first.respond_to?(:simple_calendar_start_time)
        events.select do |e|
          current_date == e.send(:simple_calendar_start_time).in_time_zone(@timezone).to_date
        end.sort_by(&:simple_calendar_start_time)
     else
        events
    end
 end

May be its stupid question or i dont know and i also tried tracing using pry for this method but i cannot understand how here sort_by works ..! Do you mind give me some idea on this how events are sorting here.


The &:symbol syntax basically just turns a method name into a block to call. What it is doing is sorting the array of events by the simple_calendar_start_time column. First we select out the ones for the day in the select block and then we sort by the start time so that all the events are filtered and in order.

That make more sense?


I didnt know how i missed this part. Thanks


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.