Ask A Question

Notifications

You’re not receiving notifications from this thread.

Can Anyone explain me these ruby codes?

aysha asked in Rails

Recently I am started learning ruby on rails. I am doing a project on ruby on rails. In my project there are some code in application.controller.rb . But I am not understanding the meaning of the code. Can anyone please explain me these codes?

def new_year_holidays?
    t = Time.now

    @notification = t >= Rails.application.config.new_year_holidays_start_at &&
                    t <= Rails.application.config.new_year_holidays_finish_at
    start_date = Rails.application.config.new_year_holidays_start_at 
    end_date   = Rails.application.config.new_year_holidays_finish_at
    @new_year_holidays_start_at = start_date.strftime("%Y年%m月%d日(#{%w(日 月 火 水 木 金 土)[start_date.wday]})")
    @new_year_holidays_finish_at = end_date.strftime("%Y年%m月%d日(#{%w(日 月 火 水 木 金 土)[end_date.wday]})")
  end
Reply

This code is pretty dirty. Basically the @notification instance variable gets set to true or false if the notification is due. It also sets to other instance vars for formatting reasons but it's not a great way coding this. Somewhere else those instance vars used to check if notification should be sent and to show the formatted dates. If you just started learning make sure you find nice materials to learn from. This is definitely not one of those.

Reply
Join the discussion
Create an account Log in

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

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

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