This is Ruby, where everything is an expression. If you're going to be clever, 
why not go all the way?

---
def is_macruby?; defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'; end

if is_macruby?
  framework 'Foundation'
else
  require 'uri'
  COMPONENTS = [:scheme, :userinfo, :host, :port, :registry,
                :path, :opaque, :query, :fragment]
end

class HackURI < (is_macruby? ? NSURL : URI::HTTP)
  def initialize(uri)
    if is_macruby?
      self.initWithString(uri)
    else
      u = URI.parse(uri)
      super(*COMPONENTS.map { |c| u.send(c) })
    end
  end

  def what_am_I?
    puts self.class.ancestors
  end
end

h = HackURI.new('http://www.macruby.org')
h.what_am_I?
---

Although, you may want to rethink your strategy here…there's a REALLY BIG 
impedance mismatch between the way that the URI module is designed and the way 
NSURL works. Anyway, hope this helps…

Cheers,

Josh


On Oct 7, 2010, at 8:21 PM, Mark Rada wrote:

> A more efficient version of that class:
> 
> class HackURI
>       if RUBY_ENGINE == 'macruby'
>               def initialize(uri)
>                       @url = NSURL.URLWithString uri
>               end
>       else
>               def initialize(uri)
>                       @url = URI.parse uri
>               end
>       end
> 
>       def method_missing(method, *args)
>               @url.send(method, *args)
>       end
> end
> 
> 
> On 2010-10-07, at 11:11 PM, Mark Rada wrote:
> 
>> Hi devs,
>> 
>> I am trying to write a gem so that it will run on MRI and MacRuby, but still 
>> be able to take advantage of MacRuby things like Cocoa and GCD.
>> 
>> I was playing around with some URI related stuff and I cannot find much 
>> documentation on whether NSURL and URI objects have been bridged for MacRuby?
>> 
>> If they aren't bridged, I was thinking I could just hack together an empty 
>> class that just chooses if it inherits from NSURL or URI at run time.
>> Something like this:
>> 
>>      require 'uri'
>>      
>>      class HackURI 
>>              def initialize(uri)
>>                       if RUBY_ENGINE == 'macruby'
>>                              @url = NSURL.URLWithString uri
>>                       else
>>                              @url = URI.parse uri
>>                      end
>>               end
>>      
>>              def method_missing(method, *args)
>>                      @url.send(method, *args)
>>              end
>>      end 
>> 
>> --
>> Mark Rada
>> 
>> _______________________________________________
>> MacRuby-devel mailing list
>> [email protected]
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
> 
> _______________________________________________
> MacRuby-devel mailing list
> [email protected]
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

_______________________________________________
MacRuby-devel mailing list
[email protected]
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

Reply via email to