Splitting up of a certain method into two not working (for me)

Hi,

I鈥檝e got a problem with splitting up my below stated method into two. I鈥檓 on Ruby 2.3.1 using the Holidays Gem, but I think this is probably more related to me not spotting the obvious :wink:

The following implementation works correctly, producing 21 business days for December 2016 (in Germany), for instance:

路路路

~~~
require 'date'
require 'holidays'

class DateOperations
聽聽def self.number_of_business_days_between(start_date, end_date)
聽聽聽聽week_days = week_days_between(start_date, end_date)
聽聽聽聽business_days = []
聽聽聽聽week_days.each do |b|
聽聽聽聽聽聽business_days << b unless holiday?(b)
聽聽聽聽end
聽聽聽聽business_days.length
聽聽end

聽聽def self.week_days_between(start_date, end_date)
聽聽聽聽week_day_nbrs = (1..5)
聽聽聽聽(start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
聽聽end

聽聽def self.holiday?(date)
聽聽聽聽return false if Holidays.on(date, :de, :informal).empty?
聽聽聽聽true
聽聽end
end
~~~

When delegating gathering of the array with the business days to another method, everything else staying the same, number_of_business_days_between results in 22 for December 2016 in Germany instead of 21:

~~~
聽聽def self.number_of_business_days_between(start_date, end_date)
聽聽聽聽business_days = business_days_between(start_date, end_date)
聽聽聽聽business_days.length
聽聽end

聽聽def self.business_days_between(start_date, end_date)
聽聽聽聽week_days = week_days_between(start_date, end_date)
聽聽聽聽business_days = []
聽聽聽聽week_days.each do |b|
聽聽聽聽聽聽business_days << b unless holiday?(b)
聽聽聽聽end
聽聽end
~~~

From my point of view both implementations of number_of_business_days_between should work exactly the same, unfortunately not for me - any ideas on this?

Many thanks!

Cheers,
Michael

Your `def self.business_days_between(start_date, end_date)` must return
`business_days.length`, right now it returns `week_days` instead.

路路路

On 14 June 2016 at 16:52:12, Michael Schwarze (michael@schwarze-web.de) wrote:

Hi,

I鈥檝e got a problem with splitting up my below stated method into two. I鈥檓
on Ruby 2.3.1 using the Holidays Gem, but I think this is probably more
related to me not spotting the obvious :wink:

The following implementation works correctly, producing 21 business days
for December 2016 (in Germany), for instance:

~~~
require 'date'
require 'holidays'

class DateOperations
def self.number_of_business_days_between(start_date, end_date)
week_days = week_days_between(start_date, end_date)
business_days = []
week_days.each do |b|
business_days << b unless holiday?(b)
end
business_days.length
end

def self.week_days_between(start_date, end_date)
week_day_nbrs = (1..5)
(start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
end

def self.holiday?(date)
return false if Holidays.on(date, :de, :informal).empty?
true
end
end
~~~

When delegating gathering of the array with the business days to another
method, everything else staying the same, number_of_business_days_between
results in 22 for December 2016 in Germany instead of 21:

~~~
def self.number_of_business_days_between(start_date, end_date)
business_days = business_days_between(start_date, end_date)
business_days.length
end

def self.business_days_between(start_date, end_date)
week_days = week_days_between(start_date, end_date)
business_days = []
week_days.each do |b|
business_days << b unless holiday?(b)
end
end
~~~

From my point of view both implementations of
number_of_business_days_between should work exactly the same, unfortunately
not for me - any ideas on this?

Many thanks!

Cheers,
Michael

It should actually return business_days. But I think there's a cleaner
implementation:

def self.business_days_between(start_date, end_date)
聽聽week_days = week_days_between(start_date, end_date)
聽聽week_days.reject {|b| holiday?(b) }
end

The intermediate array is managed by the reject method for you.

Jesus.

路路路

On Tue, Jun 14, 2016 at 4:56 PM, Michael Fellinger <m.fellinger@gmail.com> wrote:

Your `def self.business_days_between(start_date, end_date)` must return
`business_days.length`, right now it returns `week_days` instead.

Michael

Thanks, that was the point I was missing; the block given to week_day.each of course does not get returned!

Cheers,
Michael

路路路

Am 14.06.2016 um 16:56 schrieb Michael Fellinger <m.fellinger@gmail.com>:

Your `def self.business_days_between(start_date, end_date)` must return `business_days.length`, right now it returns `week_days` instead.

On 14 June 2016 at 16:52:12, Michael Schwarze (michael@schwarze-web.de) wrote:

Hi,

I鈥檝e got a problem with splitting up my below stated method into two. I鈥檓 on Ruby 2.3.1 using the Holidays Gem, but I think this is probably more related to me not spotting the obvious :wink:

The following implementation works correctly, producing 21 business days for December 2016 (in Germany), for instance:

~~~
require 'date'
require 'holidays'

class DateOperations
def self.number_of_business_days_between(start_date, end_date)
week_days = week_days_between(start_date, end_date)
business_days = []
week_days.each do |b|
business_days << b unless holiday?(b)
end
business_days.length
end

def self.week_days_between(start_date, end_date)
week_day_nbrs = (1..5)
(start_date..end_date).select { |w| week_day_nbrs.include?(w.wday) }
end

def self.holiday?(date)
return false if Holidays.on(date, :de, :informal).empty?
true
end
end
~~~

When delegating gathering of the array with the business days to another method, everything else staying the same, number_of_business_days_between results in 22 for December 2016 in Germany instead of 21:

~~~
def self.number_of_business_days_between(start_date, end_date)
business_days = business_days_between(start_date, end_date)
business_days.length
end

def self.business_days_between(start_date, end_date)
week_days = week_days_between(start_date, end_date)
business_days = []
week_days.each do |b|
business_days << b unless holiday?(b)
end
end
~~~

From my point of view both implementations of number_of_business_days_between should work exactly the same, unfortunately not for me - any ideas on this?

Many thanks!

Cheers,
Michael

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Mit freundlichem Gru脽
Michael Schwarze

Jesus

Thanks a lot to you, too! I can see my mistake now; that鈥檚 why I鈥檓 a friend of explicit returns :wink: I really like your cleaner approach.

Cheers,
Michael

路路路

Am 14.06.2016 um 17:01 schrieb Jes煤s Gabriel y Gal谩n <jgabrielygalan@gmail.com>:

On Tue, Jun 14, 2016 at 4:56 PM, Michael Fellinger > <m.fellinger@gmail.com> wrote:

Your `def self.business_days_between(start_date, end_date)` must return
`business_days.length`, right now it returns `week_days` instead.

It should actually return business_days. But I think there's a cleaner
implementation:

def self.business_days_between(start_date, end_date)
week_days = week_days_between(start_date, end_date)
week_days.reject {|b| holiday?(b) }
end

The intermediate array is managed by the reject method for you.

Jesus.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>

Mit freundlichem Gru脽
Michael Schwarze