[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Class & modifiers modifiers

Yukihiro Matsumoto

1/25/2007 6:17:00 PM

Hi,

In message "Re: Class & modifiers modifiers"
on Fri, 26 Jan 2007 01:13:25 +0900, Miquel <ktalanet@yahoo.es> writes:

|> public. I don't recommend to override protected method though.
|
|Why not?

Because protected methods are called only from subclass of defining
class. That means overriding changes the restriction scope of the
method.

|You might have a protected method in a module which has no code or
|only an exception (something like an abstract method in Java/C#) and has
|a different behaviour in the different classes which include it. In this
|case it must have been overriden.
|
|What do you think about this?

As far as current protected behavior remains, I'd recommend to prepare
separate method to override in the subclass.

matz.

3 Answers

Miquel Oliete

1/26/2007 11:10:00 AM

0

EL Fri, 26 Jan 2007 03:16:39 +0900
Yukihiro Matsumoto <matz@ruby-lang.org> escrigué:

> Hi,
>
> In message "Re: Class & modifiers modifiers"
> on Fri, 26 Jan 2007 01:13:25 +0900, Miquel <ktalanet@yahoo.es>
> writes:
>
> |> public. I don't recommend to override protected method though.
> |
> |Why not?
>
> Because protected methods are called only from subclass of defining
> class. That means overriding changes the restriction scope of the
> method.
>
> |You might have a protected method in a module which has no code or
> |only an exception (something like an abstract method in Java/C#) and
> has |a different behaviour in the different classes which include it.
> In this |case it must have been overriden.
> |
> |What do you think about this?
>
> As far as current protected behavior remains, I'd recommend to prepare
> separate method to override in the subclass.
>
Excuse me if I'm getting boring but I don't know if I explained myself
well, here is an example:

module Shape

def name
#raise a not implemented method.
end

def printName()
puts name
end

end


class Square
include Shape

def name
return 'Square'
end

end

class Circle
include Shape

def name
return 'Circle'
end

end

I know that it could be written, for example, as a attr_reader and set
it in the initialize, but this is the idea I want to express (I think
its so useful).

Or maybe you want to say that it's better to code an intermidiate class
between de module and the classes (or even instead of the module) and
declare there the "abstract" method?

Sorry, but I'm so used to Java and C#.

Kind regards


Miquel

> matz.
>


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
http://es.voice...

Miquel Oliete

1/26/2007 11:50:00 AM

0

EL Fri, 26 Jan 2007 20:23:36 +0900
"Robert Dober" <robert.dober@gmail.com> escrigué:

> On 1/26/07, Miquel <ktalanet@yahoo.es> wrote:
> >
> > EL Fri, 26 Jan 2007 03:16:39 +0900
> > Yukihiro Matsumoto <matz@ruby-lang.org> escrigué:
> >
> > > Hi,
> > >
> > > In message "Re: Class & modifiers modifiers"
> > > on Fri, 26 Jan 2007 01:13:25 +0900, Miquel <ktalanet@yahoo.es>
> > > writes:
> > >
> > > |> public. I don't recommend to override protected method though.
> > > |
> > > |Why not?
> > >
> > > Because protected methods are called only from subclass of
> > > defining class. That means overriding changes the restriction
> > > scope of the method.
> > >
> > > |You might have a protected method in a module which has no code
> > > or |only an exception (something like an abstract method in
> > > Java/C#) and has |a different behaviour in the different classes
> > > which include it. In this |case it must have been overriden.
> > > |
> > > |What do you think about this?
> > >
> > > As far as current protected behavior remains, I'd recommend to
> > > prepare separate method to override in the subclass.
> > >
> > Excuse me if I'm getting boring but I don't know if I explained
> > myself well, here is an example:
> >
> > module Shape
> >
> > def name
> > #raise a not implemented method.
> > end
> >
> > def printName()
> > puts name
> > end
> >
> > end
> >
> >
> > class Square
> > include Shape
> >
> > def name
> > return 'Square'
> > end
> >
> > end
> >
> > class Circle
> > include Shape
> >
> > def name
> > return 'Circle'
> > end
> >
> > end
> >
> > I know that it could be written, for example, as a attr_reader and
> > set it in the initialize, but this is the idea I want to express (I
> > think its so useful).
> >
> > Or maybe you want to say that it's better to code an intermidiate
> > class between de module and the classes (or even instead of the
> > module) and declare there the "abstract" method?
> >
> > Sorry, but I'm so used to Java and C#.
> >
> > Kind regards
> >
> >
> > Miquel
>
>
> May I suggest just not to define Shape#name, I believe that would be
> the most ruby like way to do this.
>
> Imagine that you forget to define it
> Hexagon = Class.new Shape # guess where I live ;)
I'm sorry, but Shape is a module (it can not be create an object using
it).
>
> Hexagon.new.print_name ===> NoMethodError
>
> AFAIU that is pretty much the behavior you want
>
Thanks a lot for your suggestions, I'll try to rebuild my ideas using
Ruby.
>
> HTH
> Robert
Kind regards
>
> > matz.
> > >
> >
> >
> > ______________________________________________
> > LLama Gratis a cualquier PC del Mundo.
> > Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
> > http://es.voice...
> >
> >
>
>


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
http://es.voice...

Miquel Oliete

1/26/2007 12:33:00 PM

0

EL Fri, 26 Jan 2007 20:49:46 +0900
Miquel <ktalanet@yahoo.es> escrigué:

> EL Fri, 26 Jan 2007 20:23:36 +0900
> "Robert Dober" <robert.dober@gmail.com> escrigué:
>
> > On 1/26/07, Miquel <ktalanet@yahoo.es> wrote:
> > >
> > > EL Fri, 26 Jan 2007 03:16:39 +0900
> > > Yukihiro Matsumoto <matz@ruby-lang.org> escrigué:
> > >
> > > > Hi,
> > > >
> > > > In message "Re: Class & modifiers modifiers"
> > > > on Fri, 26 Jan 2007 01:13:25 +0900, Miquel
> > > > <ktalanet@yahoo.es> writes:
> > > >
> > > > |> public. I don't recommend to override protected method
> > > > though. |
> > > > |Why not?
> > > >
> > > > Because protected methods are called only from subclass of
> > > > defining class. That means overriding changes the restriction
> > > > scope of the method.
> > > >
> > > > |You might have a protected method in a module which has no code
> > > > or |only an exception (something like an abstract method in
> > > > Java/C#) and has |a different behaviour in the different classes
> > > > which include it. In this |case it must have been overriden.
> > > > |
> > > > |What do you think about this?
> > > >
> > > > As far as current protected behavior remains, I'd recommend to
> > > > prepare separate method to override in the subclass.
> > > >
> > > Excuse me if I'm getting boring but I don't know if I explained
> > > myself well, here is an example:
> > >
> > > module Shape
> > >
> > > def name
> > > #raise a not implemented method.
> > > end
> > >
> > > def printName()
> > > puts name
> > > end
> > >
> > > end
> > >
> > >
> > > class Square
> > > include Shape
> > >
> > > def name
> > > return 'Square'
> > > end
> > >
> > > end
> > >
> > > class Circle
> > > include Shape
> > >
> > > def name
> > > return 'Circle'
> > > end
> > >
> > > end
> > >
> > > I know that it could be written, for example, as a attr_reader and
> > > set it in the initialize, but this is the idea I want to express
> > > (I think its so useful).
> > >
> > > Or maybe you want to say that it's better to code an intermidiate
> > > class between de module and the classes (or even instead of the
> > > module) and declare there the "abstract" method?
> > >
> > > Sorry, but I'm so used to Java and C#.
> > >
> > > Kind regards
> > >
> > >
> > > Miquel
> >
> >
> > May I suggest just not to define Shape#name, I believe that would be
> > the most ruby like way to do this.
> >
> > Imagine that you forget to define it
> > Hexagon = Class.new Shape # guess where I live ;)
> I'm sorry, but Shape is a module (it can not be create an object using
> it).
I'm sorry if I have been a little rude, it was not my intention :-)
> >
> > Hexagon.new.print_name ===> NoMethodError
> >
> > AFAIU that is pretty much the behavior you want
> >
> Thanks a lot for your suggestions, I'll try to rebuild my ideas using
> Ruby.
Bye
> >
> > HTH
> > Robert
> Kind regards
> >
> > > matz.
> > > >
> > >
> > >
> > > ______________________________________________
> > > LLama Gratis a cualquier PC del Mundo.
> > > Llamadas a fijos y m�viles desde 1 c�ntimo por minuto.
> > > http://es.voice...
> > >
> > >
> >
> >
>
>
> ______________________________________________
> LLama Gratis a cualquier PC del Mundo.
> Llamadas a fijos y móviles desde 1 céntimo por minuto.
> http://es.voice...
>


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y m?viles desde 1 c?ntimo por minuto.
http://es.voice...