[MacRuby-devel] How do I subclass Obj-C classes in MacRuby?

russell muetzelfeldt russm-macruby-devel at slofith.org
Wed May 5 00:49:01 PDT 2010


> From: Thibault Martin-Lagardette <thibault.ml at gmail.com>
> 
> Hi!
> 
> I believe the problem is that you were overriding the wrong init method. Here is what I changed to your code to make it work:
> 
> class MyNode < ODNode
>    def initWithSession(session, name:name, error:err)
>        if super
>            @session = session
>            self
>        end
>    end
> end
> 
> session = ODSession.defaultSession
> node = MyNode.nodeWithSession session, name: "/Local/Default", error: nil
> 
> I override initWithSession:name:error instead of init, and created a "MyNode" object the exact same way I would have created an ODNode :-)
> The results were, I believe, what you would expect:



Thanks, but unfortunately that doesn't actually help - there's no difference in use between that and not wrapping the object init... For example, what I'm doing currently is this -

module OpenDirectory
  class Node
    attr_reader :node
    def initialize config = { :node_name => "/Local/Default" }
      if config[:node_name].eql? "/Local/Default"
        @session = ODSession.defaultSession
      else
        @session = ODSession.sessionWithOptions config[:session_options], error:nil
      end
      @node = ODNode.nodeWithSession @session, name:config[:node_name], error:nil
    end
  end
end

so I can either

local_node = OpenDirectory::Node.new

or

remote_node = OpenDirectory::Node.new proxy_config

where proxy_config is a hash containing info for a DS remote connection to our directory master. I then access the ODNode as either local_node.node or remote_node.node where required. What I want to be able to do is just use local_node or remote_node as if they were ODNode objects (or subclasses of ODNode) to remove the ruby proxying wrapper around the core Obj-C objects, but still hide the ugliness of OpenDirectory behind some simpler Model style wrappers.

(I hope that makes sense)

cheers

Russell



More information about the MacRuby-devel mailing list