Forum: Ruby Class & modifiers modifiers

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Miquel (Guest)
on 2007-01-25 16:58
(Received via mailing list)
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
Yukihiro M. (Guest)
on 2007-01-25 17:55
(Received via mailing list)
Hi,

In message "Re: Class & modifiers modifiers"
    on Thu, 25 Jan 2007 23:58:09 +0900, Miquel 
<removed_email_address@domain.invalid>
writes:

|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 (;-) ) ?

public.  I don't recommend to override protected method though.

              matz.
Miquel (Guest)
on 2007-01-25 18:13
(Received via mailing list)
EL Fri, 26 Jan 2007 00:55:18 +0900
Yukihiro M. <removed_email_address@domain.invalid> escrigué:

> |    def b_method()
> |  end
> |
> |end
> |
> |Is B's method b_method() protected? Is it public? What the hell is
> this |method (;-) ) ?
>
> public.  I don't recommend to override protected method though.
>

Why not?

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.
>
              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
Yukihiro M. (Guest)
on 2007-01-25 20:17
(Received via mailing list)
Hi,

In message "Re: Class & modifiers modifiers"
    on Fri, 26 Jan 2007 01:13:25 +0900, Miquel 
<removed_email_address@domain.invalid>
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.
Miquel (Guest)
on 2007-01-26 13:10
(Received via mailing list)
EL Fri, 26 Jan 2007 03:16:39 +0900
Yukihiro M. <removed_email_address@domain.invalid> escrigué:

> Because protected methods are called only from subclass of defining
> 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.yahoo.com
Robert D. (Guest)
on 2007-01-26 13:23
(Received via mailing list)
On 1/26/07, Miquel <removed_email_address@domain.invalid> wrote:
> > |> public.  I don't recommend to override protected method though.
> > In this |case it must have been overriden.
>
>
>         include Shape
>
> 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 ;)

Hexagon.new.print_name ===> NoMethodError

AFAIU that is pretty much the behavior you want


HTH
Robert
Miquel (Guest)
on 2007-01-26 13:49
(Received via mailing list)
EL Fri, 26 Jan 2007 20:23:36 +0900
"Robert D." <removed_email_address@domain.invalid> escrigué:

> > >
> > > Java/C#) and has |a different behaviour in the different classes
> > module Shape
> >
> > class Circle
> > think its so useful).
> >         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
> >
>
>


______________________________________________
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 (Guest)
on 2007-01-26 14:34
(Received via mailing list)
EL Fri, 26 Jan 2007 20:49:46 +0900
Miquel <removed_email_address@domain.invalid> escrigué:

> > > > In message "Re: Class & modifiers modifiers"
> > > >
> > > Excuse me if I'm getting boring but I don't know if I explained
> > >         end
> > >
> > >
> > > Kind regards
> 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
> > > LLama Gratis a cualquier PC del Mundo.
> Llamadas a fijos y móviles desde 1 céntimo por minuto.
> http://es.voice.yahoo.com
>


______________________________________________
LLama Gratis a cualquier PC del Mundo.
Llamadas a fijos y móviles desde 1 céntimo por minuto.
http://es.voice.yahoo.com
Robert D. (Guest)
on 2007-01-26 14:40
(Received via mailing list)
>
> >
> > 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).


You
Robert D. (Guest)
on 2007-01-26 14:41
(Received via mailing list)
On 1/26/07, Robert D. <removed_email_address@domain.invalid> wrote:
>
> You are completely right I hate to be imprecise sorry for that, but I does
> not really matter, it is the same pattern.
>

¡Hasta la proxima!
Robert
This topic is locked and can not be replied to.