Class & modifiers modifiers

Discussion in 'Ruby' started by Miquel, Jan 25, 2007.

  1. Miquel

    Miquel Guest

    Hi

    I want to make a question to all the ruby-lang people (I hope that
    somebody answered it).

    I'm writing a database GUI and I'm coding the backend using modules
    and classes.

    If I have this situation:

    module A

    protected

    def b_method()
    return 'foo->A'
    end

    end

    class B
    include A

    def b_method()
    return 'foo->B'
    end

    end

    Is B's method b_method() protected? Is it public? What the hell is this
    method (;-) ) ?


    Thanks in advance

    Kind regards


    Miquel


    ______________________________________________
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    http://es.voice.yahoo.com
     
    Miquel, Jan 25, 2007
    #1
    1. Advertising

  2. Miquel

    Miquel Guest

    EL Fri, 26 Jan 2007 00:55:18 +0900
    Yukihiro Matsumoto <> escrigu=C3=A9:

    > Hi,
    >=20
    > In message "Re: Class & modifiers modifiers"
    > on Thu, 25 Jan 2007 23:58:09 +0900, Miquel <>
    > writes:
    >=20
    > |module A
    > |
    > | protected
    > |
    > | def b_method()
    > | return 'foo->A'
    > | end
    > |
    > |end
    > |
    > |class B
    > | include A
    > |
    > | def b_method()
    > | return 'foo->B'
    > | end
    > |
    > |end
    > |
    > |Is B's method b_method() protected? Is it public? What the hell is
    > this |method (;-) ) ?
    >=20
    > public. I don't recommend to override protected method though.
    >=20


    Why not?=20

    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?

    Btw, congratulations for this awesome language you have written.


    > matz.
    >=20

    Miquel


    ______________________________________________
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    http://es.voice.yahoo.com
     
    Miquel, Jan 25, 2007
    #2
    1. Advertising

  3. Miquel

    Miquel Guest

    EL Fri, 26 Jan 2007 03:16:39 +0900
    Yukihiro Matsumoto <> escrigu=C3=A9:

    > Hi,
    >=20
    > In message "Re: Class & modifiers modifiers"
    > on Fri, 26 Jan 2007 01:13:25 +0900, Miquel <>
    > writes:
    >=20
    > |> public. I don't recommend to override protected method though.
    > |
    > |Why not?=20
    >=20
    > Because protected methods are called only from subclass of defining
    > class. That means overriding changes the restriction scope of the
    > method.
    >=20
    > |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?
    >=20
    > As far as current protected behavior remains, I'd recommend to prepare
    > separate method to override in the subclass.
    >=20

    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.
    >=20



    ______________________________________________
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    http://es.voice.yahoo.com
     
    Miquel, Jan 26, 2007
    #3
  4. Miquel

    Miquel Guest

    EL Fri, 26 Jan 2007 20:23:36 +0900
    "Robert Dober" <> escrigu=C3=A9:

    > On 1/26/07, Miquel <> wrote:
    > >
    > > EL Fri, 26 Jan 2007 03:16:39 +0900
    > > Yukihiro Matsumoto <> escrigu=C3=A9:
    > >
    > > > Hi,
    > > >
    > > > In message "Re: Class & modifiers modifiers"
    > > > on Fri, 26 Jan 2007 01:13:25 +0900, Miquel <>
    > > > 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

    >=20
    >=20
    > May I suggest just not to define Shape#name, I believe that would be
    > the most ruby like way to do this.
    >=20
    > Imagine that you forget to define it
    > Hexagon =3D Class.new Shape # guess where I live ;)

    I'm sorry, but Shape is a module (it can not be create an object using
    it).
    >=20
    > Hexagon.new.print_name =3D=3D=3D> NoMethodError
    >=20
    > AFAIU that is pretty much the behavior you want
    >=20

    Thanks a lot for your suggestions, I'll try to rebuild my ideas using
    Ruby.
    >=20
    > HTH
    > Robert

    Kind regards
    >=20
    > > matz.
    > > >

    > >
    > >
    > > ______________________________________________
    > > LLama Gratis a cualquier PC del Mundo.
    > > Llamadas a fijos y m=EF=BF=BDviles desde 1 c=EF=BF=BDntimo por minuto.
    > > http://es.voice.yahoo.com
    > >
    > >

    >=20
    >=20



    ______________________________________________
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    http://es.voice.yahoo.com
     
    Miquel, Jan 26, 2007
    #4
  5. Miquel

    Miquel Guest

    EL Fri, 26 Jan 2007 20:49:46 +0900
    Miquel <> escrigu=C3=83=C2=A9:

    > EL Fri, 26 Jan 2007 20:23:36 +0900
    > "Robert Dober" <> escrigu=C3=83=C2=A9:
    >=20
    > > On 1/26/07, Miquel <> wrote:
    > > >
    > > > EL Fri, 26 Jan 2007 03:16:39 +0900
    > > > Yukihiro Matsumoto <> escrigu=C3=83=C2=A9:
    > > >
    > > > > Hi,
    > > > >
    > > > > In message "Re: Class & modifiers modifiers"
    > > > > on Fri, 26 Jan 2007 01:13:25 +0900, Miquel
    > > > > <> 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

    > >=20
    > >=20
    > > May I suggest just not to define Shape#name, I believe that would be
    > > the most ruby like way to do this.
    > >=20
    > > Imagine that you forget to define it
    > > Hexagon =3D 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 :)
    > >=20
    > > Hexagon.new.print_name =3D=3D=3D> NoMethodError
    > >=20
    > > AFAIU that is pretty much the behavior you want
    > >=20

    > Thanks a lot for your suggestions, I'll try to rebuild my ideas using
    > Ruby.

    Bye
    > >=20
    > > HTH
    > > Robert

    > Kind regards
    > >=20
    > > > matz.
    > > > >
    > > >
    > > >
    > > > ______________________________________________
    > > > LLama Gratis a cualquier PC del Mundo.
    > > > Llamadas a fijos y m=C3=AF=C2=BF=C2=BDviles desde 1 c=C3=AF=C2=BF=C2=

    =BDntimo por minuto.
    > > > http://es.voice.yahoo.com
    > > >
    > > >

    > >=20
    > >=20

    >=20
    > =09
    > ______________________________________________=20
    > LLama Gratis a cualquier PC del Mundo.=20
    > Llamadas a fijos y m=C3=B3viles desde 1 c=C3=A9ntimo por minuto.=20
    > http://es.voice.yahoo.com
    >=20



    ______________________________________________
    LLama Gratis a cualquier PC del Mundo.
    Llamadas a fijos y móviles desde 1 céntimo por minuto.
    http://es.voice.yahoo.com
     
    Miquel, Jan 26, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shila
    Replies:
    3
    Views:
    684
    Tor Iver Wilhelmsen
    Feb 24, 2004
  2. Razvan
    Replies:
    11
    Views:
    934
    Andrew Thompson
    Jul 17, 2004
  3. Indudhar
    Replies:
    4
    Views:
    379
    Patrick May
    Oct 21, 2005
  4. Tony Morris

    Local Classes - Access Modifiers?

    Tony Morris, Jan 14, 2006, in forum: Java
    Replies:
    10
    Views:
    945
    Stefan Schulz
    Jan 16, 2006
  5. man4*.*

    access modifiers

    man4*.*, Nov 18, 2006, in forum: Java
    Replies:
    4
    Views:
    369
    man4*.*
    Nov 19, 2006
Loading...

Share This Page