# Workarounds for replacing self

**URL:** <https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600>\
**Category:** Ruby\
**Created:** [May 22, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600 "2009-05-22T19:19:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 22, 2009, 7:19pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/1 "2009-05-22T19:19:37Z")

</div>

I’m attempting to build an ETA class, essentially identical to Time, but  
where new accepts a number of seconds from now and generates the  
underlying Time object accordingly. Obviously I can’t re-assign self, so  
how should I do this?

## What I’d like to do:

## class ETA \< Time def initialize(seconds) self = Time.new + seconds end end

Any help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 22, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/2 "2009-05-22T19:34:21Z")

</div>

On 22 May 2009, at 18:19, Jp Hastings-spital wrote:

> ## def initialize(seconds) self = Time.new + seconds end end
> 
> Any help would be much appreciated!

How about something as simple as:

class ETA \< Time  
def self.new  
ETA.at Time.now + seconds  
end  
end

Ellie

## Eleanor McHugh Games With Brains [http://slides.games-with-brains.net](http://slides.games-with-brains.net)

raise ArgumentError unless @reality.responds\_to? :reason

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 22, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/3 "2009-05-22T19:39:35Z")

</div>

Excellent! I had difficulty getting that to work with initialize because  
its an instance method - thanks very much!

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 24, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/4 "2009-05-24T17:45:34Z")

</div>

On 22.05.2009 19:29, Eleanor McHugh wrote:

> > class ETA \< Time  
> > def initialize(seconds)  
> > self = Time.new + seconds  
> > end  
> > end

> How about something as simple as:
> 
> class ETA \< Time  
> def self.new  
> ETA.at Time.now + seconds  
> end  
> end

Actually, what do we need a new class for? Basically this will do as  
well, unless there are more methods added to ETA that we do not yet  
know:

def Time.future(seconds)  
now + seconds  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 24, 2009, 6:06pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/5 "2009-05-24T18:06:58Z")

</div>

There were a few other methods I needed - have a look:

> <https://gist.github.com/jphastings/116293>

I realise its potentially overkill making a whole new class for this,  
but some of the scripts I’m now using this in have boths times and ETAs,  
and a quick ‘is\_a?’ allows me to distinguish them easily.  
Let me know if you think you can improve it!

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 24, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/6 "2009-05-24T19:02:22Z")

</div>

On 24.05.2009 18:07, Jp Hastings-spital wrote:

> There were a few other methods I needed - have a look:  
> [An ETA class to allow intuitive access to an estimated time of arrival · GitHub](http://gist.github.com/116293)
> 
> I realise its potentially overkill making a whole new class for this,  
> but some of the scripts I’m now using this in have boths times and ETAs,  
> and a quick ‘is\_a?’ allows me to distinguish them easily.  
> Let me know if you think you can improve it!

Not much change - you don’t need all the “selfs”:

class ETA \< Time

# Takes a number of seconds until the event

def self.relative(seconds)  
at Time.now.to\_i + seconds  
end

# Requires [116290’s gists · GitHub](http://gist.github.com/116290)

def to\_s  
roughly  
end

# Gives a full textual representation of the time expected time of

arrival (Time.rfc2822)  
def eta  
rfc2822  
end

# Has the eta passed?

def arrived?  
self \< Time.now  
end  
end

And you can directly compare Time and ETA objects.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 24, 2009, 8:00pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/7 "2009-05-24T20:00:40Z")

</div>

On 24.05.2009 19:39, Jp Hastings-spital wrote:

> method to self.relative? Is that personal preference, or is there a  
> reason you’ve kept Time’s original .new and .now?

Your ETA.new does something different than Time.new and because of that  
a new name would help avoid confusion. Also, by that means you can  
still use the “old” new with the old semantics, i.e. you can do ETA.new  
and get an instance with the current timestamp.

You could even put relative in class Time and use it both ways:

def Time.relative(sec)  
at now + sec  
end

class ETA \< Time

# other instance methods

end

t = Time.relative 123  
e = ETA.relative 123

p t, t.class, e, e.class

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 24, 2009, 7:39pm UTC](https://www.ruby-forum.com/t/workarounds-for-replacing-self/168600/8 "2009-05-24T19:39:53Z")

</div>

Robert K. wrote:

> class ETA \< Time
> 
> # Takes a number of seconds until the event
> 
> def self.relative(seconds)  
> at Time.now.to\_i + seconds  
> end  
> …  
> end

Thanks for the tips! Out of interest, why did you change my self.new  
method to self.relative? Is that personal preference, or is there a  
reason you’ve kept Time’s original .new and .now?
