[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Derived-class initialization

Bryan Richardson

3/18/2009 2:25:00 PM

Hello all,

I'm having some trouble with extending an existing Ruby class in order
to add functionality. Here's some example code:

class Graph
....
....
def subgraph(*args)
graph = self.new
....
....
return graph
end
end

class AwesomeGraph < Graph
....
....
def do_something
....
....
end
end

g = AwesomeGraph.new
a = g.subgraph(g.nodes)
a.do_something // ERROR! No Graph#do_something exists!

I was hoping 'self.new' would take into account the derived class used
upon creation, but it doesn't.

Any suggestions?

--
Thanks!
Bryan
--
Posted via http://www.ruby-....

2 Answers

James Coglan

3/18/2009 2:33:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

2009/3/18 Bryan Richardson <btrichardson@gmail.com>

> Hello all,
>
> I'm having some trouble with extending an existing Ruby class in order
> to add functionality. Here's some example code:
>
> class Graph
> ....
> ....
> def subgraph(*args)
> graph = self.new
> ....
> ....
> return graph
> end
> end



This looks suspect, 'self' in this method looks like it will refer to the
Graph instance, not the Graph class. Try self.class.new instead.

--
James Coglan
http://github.c...

Bryan Richardson

3/18/2009 2:48:00 PM

0

Yep... that worked. :)

--
Thanks!
Bryan

James Coglan wrote:
> 2009/3/18 Bryan Richardson <btrichardson@gmail.com>
>
>> ....
>> ....
>> return graph
>> end
>> end
>
>
>
> This looks suspect, 'self' in this method looks like it will refer to
> the
> Graph instance, not the Graph class. Try self.class.new instead.

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