Skip to content

Commit

Permalink
Remove DateTime implementation, remove Delegation
Browse files Browse the repository at this point in the history
DateTime inherits from Date, so we don't need to implement in both. And
the DateTime implementation was wrong, anyway.

Using SimpleDelegation for Gigaseconds was a bad idea as delegation
wasn't needed. Imagine this code:

```ruby
d = Gigasecond.new(Date.today)
irb(main):011:0> d.date
irb(main):012:0> d.to_time
```

`date` returns a date 32 years in the future, while `to_time` returns
today. Bad idea.
  • Loading branch information
whit0694 committed Jun 10, 2014
1 parent 50fd3d9 commit 17021aa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions ruby/gigasecond/gigasecond.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
require 'delegate'
class Gigasecond < SimpleDelegator
def date
__getobj__.gigaseconds_since(1)
class Gigasecond
def initialize(start_date)
self.start_date = start_date
end
end

class DateTime
def gigaseconds_since(multiple)
self + multiple.gigaseconds
def date
start_date.gigaseconds_since(1)
end

private

attr_accessor :start_date
end

class Date
Expand Down
2 changes: 1 addition & 1 deletion ruby/gigasecond/gigasecond_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_datetime
1000.times do |x|
random_datetime = Time.at(rand * Time.now.to_i).to_datetime
random_gigaseconds = rand(1000)
expected = random_datetime + (10**9 * random_gigaseconds)
expected = random_datetime + (10**9 * random_gigaseconds / (24 * 60 * 60))

assert_equal expected, random_datetime.gigaseconds_since(random_gigaseconds), "DateTime: #{random_datetime} plus #{random_gigaseconds} gigaseconds should be #{expected} using gigaseconds_since"
end
Expand Down

0 comments on commit 17021aa

Please sign in to comment.