[MacRuby-devel] super method

Thibault Martin-Lagardette thibault.ml at gmail.com
Wed Sep 8 11:22:37 PDT 2010


> 3) If I have an empty init, e.g.,
> 
> 	def init; super; self; end
> 
> 	Can I always delete  the empty init and get the same result through inheritance?



Yes, you can omit the #init declaration in this case, it works like a charm, because every Obj-C object (and thus every MacRuby object) inherits from NSObject, that has its own -init method.
What this means is if you don't define yours, calling -init on your object will call NSObject's -init, just like in Obj-C :-)

class Hello
    def world
      puts "Hello world!"
    end
end

Hello.alloc.init.world


> I don't think I've seen any examples using the initWith method. Is that method called after init?

Simply something of the like:

- (id)initWithDelegate:(id)someDelegate
{
    [self setDelegate:someDelegate];
}

or

- (id)initWithColor:(MRColor)color andBorderSize:(int)borderSize
{
    [self setCubeColor:color];
    [self setBorderSize:borderSize];
}

Hope this helped :-)

-- 
Thibault Martin-Lagardette



On Sep 8, 2010, at 09:22, Matt Aimonetti wrote:

> 1) Does MacRuby distinguish between Objective C subclasses and other Ruby classes or are all classes treated the same?
> Not really but Cocoa classes expect to be returned self in the init process and the Cocoa convention is not to overwrite init but to create your own initializer using the initWith pattern. In Ruby, #initialize is often overwritten.
> 
> 2) Can my inheritance path be arbitrarily long before I inherit from NSObject or an NS subclass?
> Yes, consider the following example:
> 
> > class Foo; end; class Bar < Foo; end; class Baz < Bar; end; class Bob < Baz; end
> => nil
> > Bob.ancestors
> => [Bob, Baz, Bar, Foo, NSObject, Kernel]
> 
> 3) If I have an empty init, e.g.,  def init; super; self; end
>        Can I always delete  the empty init and get the same result through inheritance?
> 
> I am not sure what you mean, sorry :(
> 
> - Matt
> 
> 
> On Wed, Sep 8, 2010 at 8:50 AM, Robert Rice <rice.audio at pobox.com> wrote:
> Thanks Matt:
> 
> Good tutorial on super. Maybe someone could add a keyword section to ruby-doc.
> 
> This leads me to a couple more questions on super and MacRuby inheritance:
> 
> 1) Does MacRuby distinguish between Objective C subclasses and other Ruby classes or are all classes treated the same?
> 
> 2) Can my inheritance path be arbitrarily long before I inherit from NSObject or an NS subclass?
> 
> 3) If I have an empty init, e.g.,
> 
>        def init; super; self; end
> 
>        Can I always delete  the empty init and get the same result through inheritance?
> 
> Bob Rice
> 
> 
> On Sep 7, 2010, at 4:13 PM, Matt Aimonetti wrote:
> 
> > Satish has a good blog post on the matter: http://rubylearning.com/satishtalim/ruby_overriding_methods.html
> >
> > You can certainly call super in your subclass before making any modifications or calling super based on a condition.
> >
> > I hope it helps,
> >
> > - Matt
> >
> > Sent from my iPhone
> >
> > On Sep 7, 2010, at 12:53, Robert Rice <rice.audio at pobox.com> wrote:
> >
> >> Thanks Matt:
> >>
> >> I didn't see super in the ruby-doc.org/ruby-1.9/index.html unless super is short for superclass.
> >>
> >> Can I reach a superclass method without having the message go first to my subclass override of the method?
> >>
> >> Bob Rice
> >>
> >>
> >> On Sep 7, 2010, at 3:26 PM, Matt Aimonetti wrote:
> >>
> >>> No it's not unique to MacRuby (Ruby and Obj-C support that feature) and yes you can pass other arguments :)
> >>>
> >>> - Matt
> >>>
> >>> Sent from my iPhone
> >>>
> >>> On Sep 7, 2010, at 12:16, Robert Rice <rice.audio at pobox.com> wrote:
> >>>
> >>>> Questions on the super method:
> >>>>
> >>>> Is the super method unique to MacRuby?
> >>>>
> >>>> super forwards the current message to the superclass with the same method name and arguments. Is there a way to send a message to the superclass method with different arguments either from within the subclass method of the same name or from outside the subclass method of the same name to bypass the subclass method?
> >>>>
> >>>> Thanks,
> >>>> Bob Rice
> >>>>
> >>>> _______________________________________________
> >>>> MacRuby-devel mailing list
> >>>> MacRuby-devel at lists.macosforge.org
> >>>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> >>> _______________________________________________
> >>> MacRuby-devel mailing list
> >>> MacRuby-devel at lists.macosforge.org
> >>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> >>>
> >>
> >> _______________________________________________
> >> MacRuby-devel mailing list
> >> MacRuby-devel at lists.macosforge.org
> >> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> > _______________________________________________
> > MacRuby-devel mailing list
> > MacRuby-devel at lists.macosforge.org
> > http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> >
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20100908/4345790e/attachment-0001.html>


More information about the MacRuby-devel mailing list