[MacRuby-devel] Translating Obj-C to MacRuby

Laurent Sansonetti lsansonetti at apple.com
Wed Oct 14 12:26:21 PDT 2009


Hi,

On Oct 14, 2009, at 2:34 AM, B. Ohr wrote:

> Hi,
>
> Am 14.10.2009 um 11:04 schrieb Laurent Sansonetti:
>
>> Hi,
>>
>> On Oct 14, 2009, at 1:03 AM, B. Ohr wrote:
>>
>>> Hi all!
>>>
>>> Using Cocoa in MacRuby is sometimes a hard job, because all  
>>> documentation, examples and sample code is Obj-C. For example, I  
>>> found this piece of code '[NSNumber numberWithBool:NO]' and asked  
>>> myself how to write that in Macruby.
>>
>> You can simply pass true or false and MacRuby will do the  
>> conversion for you.
>
> Oh yes, in my example project , I automatically used true and it  
> worked. But then I began asking myself, how can I be shure that this  
> is the correct way and what would an absolute beginner do and then i  
> opened macirb...  ;-)
>
>>
>>> I opened macirb and typed:
>>>
>>> > NSNumber.numberWithBool 0
>>> => false
>>>
>>> First I had to laugh and then I thought: Hey implementors of  
>>> macruby, you really did a great job!
>>
>> And NSNumbers are converted to Ruby types as you just experienced :)
>
> In the docs
>
> + (NSNumber *)numberWithBool:(BOOL)value
>
> gives a NSNumber and not a TrueClass or FalseClass as in Macruby. I  
> only wanted to say how impressed I am!

Hehe, thank you :)

>>> What do you think about a table on http://www.macruby.org/ which  
>>> lists all such short (and astonishing) examples. Or even better,  
>>> why not create a command in the services menu which translates a  
>>> Obj-C sequence (by regexp).
>>
>> I think a short tutorial/page on the website would be great,  
>> indeed. I don't believe it is easily possible to write an Objective- 
>> C -> MacRuby convertor that works most/all the time (because of the  
>> C nature of ObjC).
>>
>
> Such a cheat sheet can easily show the beauty of MacRuby and the  
> "noisiness" of Obj-C! ;-)

If you or anyone are willing to work on this, feel free and we can  
integrate it later on the website.

> I agree that a real translator is impossible. What I want is a  
> simple text processing which removes all the [foo: bar baz] and  
> replaces it with foo.bar(baz).

I see. That should be easy then :)

>>> BTW, the second example I tried is:
>>>
>>> > NSDictionary.dictionaryWithObjectsAndKeys "a", "b", nil
>>>
>>> but instead of {"b"=>"a"} I got a seg fault! (ticket is filed)
>>
>> I replied to the ticket, it seems you forgot to do `framework  
>> 'Foundation'' first.
>>
>
> Oh, my fault! But wouldn't it be better NOT to respond with a  
> segment fault (this irritated me) and instead throw an normal ruby  
> exception? (But perhaps that is impossible.)

It's unfortunately not possible, we crash when calling the method  
because we don't use the right ABI conventions (since the method is  
variadic) and there is no way we can determine that at runtime.

This is why we need to load the framework metadata (BridgeSupport  
file) in order to handle these methods.

Laurent


More information about the MacRuby-devel mailing list