Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:22:26+00:00 2026-06-14T08:22:26+00:00

The code below helps calculate business hours for completing tasks. In the calculate_deadline method,

  • 0

The code below helps calculate business hours for completing tasks. In the calculate_deadline method, in the BusinesHours Class, the two puts statements reveal this

 puts self
#<BusinessHours:0x000001008cbb70>

  puts self[start_time.to_date].inspect
#<TimeRange:0x000001008cbb48 @range=2010-06-10 09:00:00 -0700..2010-06-10 15:00:00 -0700>

I don’t understand why putting start_time.to_date beside ‘self’ in calculate_deadline changes self from BusinessHours class to TimeRange class, as the two puts statements suggest, especially since the ‘to_date’ method is part of class Time. Can you explain how this is happening?

require 'time'
require 'date'

class Time
  def to_date
    Date.new(year, month, day)
  end
end

class BusinessHours

  def initialize(time_in, time_out)
    @default_range = TimeRange.new(time_in, time_out)
    @modified_days = {}
  end

  def update(day, time_in, time_out)
    key = day.is_a?(Symbol) ? day : Date.parse(day)
    @modified_days.merge!({key => TimeRange.new(time_in, time_out)})
  end

  def closed(*days)
    days.each {|day| update(day, '0:00', '0:00')}      
  end

  def [](date)
    day_of_week = date.strftime("%a").downcase.to_sym
    range = @modified_days[date] || @modified_days[day_of_week] || @default_range
    # reset time range dates to match date param
    range.reset_date(date)
    range
  end

  def calculate_deadline(seconds, start_time)
    start_time = Time.parse(start_time)
    puts self
    puts self[start_time.to_date].inspect
    range = self[start_time.to_date]


    if range.applies?(start_time)
      start_time = [start_time, range.start].max
      available_seconds = range.stop - start_time

      return start_time + seconds if available_seconds > seconds
      seconds -= available_seconds
    end

    calculate_deadline(seconds, (start_time.to_date + 1).to_s)
  end

end

class TimeRange

  def initialize(time_in, time_out)
    @range = Time.parse(time_in)..Time.parse(time_out)

  end

  def reset_date(date)
    @range = Time.local(date.year, date.month, date.day, start.hour, start.min)..
      Time.local(date.year, date.month, date.day, stop.hour, stop.min)    
  end

  def applies?(time)
    stop > time
  end

  def stop

    @range.end
  end

  def start
    @range.begin
  end

end

k = BusinessHours.new("9:00 AM", "3:00 PM")
k.calculate_deadline(20*60*60, "Jun 7, 2010 10:45 AM")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:22:28+00:00Added an answer on June 14, 2026 at 8:22 am
    @default_range = TimeRange.new(time_in, time_out)
    

    From that line, we can see that @default_range is a TimeRange instance, and that @modified_days is an empty hash (therefore @modified_days[anything] will be nil, i.e. falsey).

    range = @modified_days[date] || @modified_days[day_of_week] || @default_range
    

    Since @modified_days[anything] is falsey, range ends up being @default_range, which, as we see above, is a TimeRange object. Your [] method on BusinessHours returns the range variable, which is a TimeRange object.

    Therefore, with self being a BusinessHours object, when you call the [] method (self[argument]), you will get a TimeRange object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# dll. The code is below: public class Calculate { public
Code below is working well as long as I have class ClassSameAssembly in same
In the below code I have two tables. I join these, perform a GROUP
I use the below Utility method to display the seconds, minutes, hours, etc gone
Could anyone help me in assessing why the code below doesn't work. I'm using
Please i need help, this code below works fine on my localhost, php5.3+ but
Any help would be appreciated, I'm trying to convert the code below to C#,
Am not that skilled at Excel/VBA and need some help. The code below (in
Can anyone help me to rewrite the below code which use a fade effect
Could someone please help me with the code for below requirement or scenarios...I achieved

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.