Ask A Question

Notifications

You’re not receiving notifications from this thread.

Clarification needed for simple_calendar sort_by method

sid d asked in General

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.

Reply

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?

Reply

I didnt know how i missed this part. Thanks

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.