[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Inheritance Question

Mike Hamilton

5/14/2007 4:12:00 PM

I'm very confused at the moment regarding inheritance. I have a class
that inherits from Time as in:

class RTDate < Time
...my methods adding to the class
end

I have the initialize method inherit from the parent class. Here is
where I get confused:
irb(main):001:0> require 'rtdate2.rb'
=> true
irb(main):002:0> r = RTDate.new
=> Mon May 14 09:12:06 PDT 2007
irb(main):003:0> r.class
=> RTDate
irb(main):004:0> s = r.yesterday
=> Sun May 13 09:12:06 PDT 2007
irb(main):005:0> s.class
=> Time
irb(main):006:0>

Here is what I don't understand - why does s become a Time object
instead of an RTDate object. If I do s = r, then I can get all of my
methods out because s becomes an RTDate object. I'm really confused
because the yesterday method is specific to my RTDate class so how can
the resulting object be a time?
Below is an excerpt from my class thus far:
class RTDate < Time
def yesterday
self - (60*60*24)
end
end

--
Posted via http://www.ruby-....

6 Answers

Sean T Allen

5/14/2007 4:19:00 PM

0

Because you are returning the result of '-'.

If you don't specifically have that return an RTDate, it will return the
result of Time '-'
which is either:

time - time => float
time - numeric => time

yours is the second.

As ruby classes are 'open' and you can add methods to them, inheritance
might not even
be how you want to approach this problem. If RTDate is adding methods
and not storing
additional instance or class data, you should just be adding yesterday
and whatever else
as part of the Time class.

Mike Hamilton wrote:
> I'm very confused at the moment regarding inheritance. I have a class
> that inherits from Time as in:
>
> class RTDate < Time
> ...my methods adding to the class
> end
>
> I have the initialize method inherit from the parent class. Here is
> where I get confused:
> irb(main):001:0> require 'rtdate2.rb'
> => true
> irb(main):002:0> r = RTDate.new
> => Mon May 14 09:12:06 PDT 2007
> irb(main):003:0> r.class
> => RTDate
> irb(main):004:0> s = r.yesterday
> => Sun May 13 09:12:06 PDT 2007
> irb(main):005:0> s.class
> => Time
> irb(main):006:0>
>
> Here is what I don't understand - why does s become a Time object
> instead of an RTDate object. If I do s = r, then I can get all of my
> methods out because s becomes an RTDate object. I'm really confused
> because the yesterday method is specific to my RTDate class so how can
> the resulting object be a time?
> Below is an excerpt from my class thus far:
> class RTDate < Time
> def yesterday
> self - (60*60*24)
> end
> end
>
>


Avdi Grimm

5/14/2007 4:20:00 PM

0

The '-' (minus) method is defined in Time, and it returns a Time instance.

--
Avdi

Mike Hamilton

5/14/2007 4:28:00 PM

0

So can I get around this by creating a '-' method in my own class or is
there a smarter way? I'm really new to this so input is very valuable!

--
Posted via http://www.ruby-....

Mike Hamilton

5/14/2007 4:56:00 PM

0

OK - I figured out what you mean! I created a library with a class
called Time and added my methods and it all worked great!!! Thanks for
the tip, I had no idea you could do that in ruby!

Sean T Allen wrote:
> Because you are returning the result of '-'.
>
> If you don't specifically have that return an RTDate, it will return the
> result of Time '-'
> which is either:
>
> time - time => float
> time - numeric => time
>
> yours is the second.
>
> As ruby classes are 'open' and you can add methods to them, inheritance
> might not even
> be how you want to approach this problem. If RTDate is adding methods
> and not storing
> additional instance or class data, you should just be adding yesterday
> and whatever else
> as part of the Time class.


--
Posted via http://www.ruby-....

Bertram Scharpf

5/14/2007 5:56:00 PM

0

Am Dienstag, 15. Mai 2007, 01:11:51 +0900 schrieb Mike Hamilton:
> I'm very confused at the moment regarding inheritance. I have a class
> that inherits from Time as in:
>
> class RTDate < Time
> ...my methods adding to the class
> end
>
> I have the initialize method inherit from the parent class. Here is
> where I get confused:
> irb(main):001:0> require 'rtdate2.rb'
> => true
> irb(main):002:0> r = RTDate.new
> => Mon May 14 09:12:06 PDT 2007
> irb(main):003:0> r.class
> => RTDate
> irb(main):004:0> s = r.yesterday
> => Sun May 13 09:12:06 PDT 2007
> irb(main):005:0> s.class
> => Time
> irb(main):006:0>
>
> Below is an excerpt from my class thus far:
> class RTDate < Time
> def yesterday
> self - (60*60*24)
> end
> end

The Operator - return a Time. Cast it to RTDate resp. RTTime.

class RTTime < Time ; def yesterday ; self - 60*60*24 ; end ; end
r = RTTime.now

r.yesterday # => Sun May 13 19:49:57 +0200 2007
r.yesterday.class # => Time

class RTTime ; def - oth ; RTTime.at super ; end ; end

r.yesterday # => Sun May 13 19:49:57 +0200 2007
r.yesterday.class # => RTTime

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Hermann Martinelli

5/14/2007 9:33:00 PM

0

Mike Hamilton wrote:
> I'm very confused at the moment regarding inheritance. I have a class
> that inherits from Time as in:
>
> class RTDate < Time
> ...my methods adding to the class
> end
>
> I have the initialize method inherit from the parent class. Here is
> where I get confused:
> irb(main):001:0> require 'rtdate2.rb'
> => true
> irb(main):002:0> r = RTDate.new
> => Mon May 14 09:12:06 PDT 2007
> irb(main):003:0> r.class
> => RTDate
> irb(main):004:0> s = r.yesterday
> => Sun May 13 09:12:06 PDT 2007
> irb(main):005:0> s.class
> => Time
> irb(main):006:0>
>
> Here is what I don't understand - why does s become a Time object

At first I did not understand why this should work at all.
You do not show where you have defined Time#yesterday.

Assuming you did use the -() method:
You inherit the -() method (the minus operator), which
by definition returns a Time object.

You can overload the minus operator or explicitly define
the yesterday method. Simple but easy to understand example:

# Code starts
class RTDate < Time
def yesterday
# Create a new (!) RTDate (!) object:
RTDate.at(self - 86400)
end
end
r = RTDate.new
puts r.yesterday, r.yesterday.class
# Code ends

Output:

Mon May 14 23:26:32 +0200 2007
Sun May 13 23:26:32 +0200 2007
RTDate