[MacRuby-devel] Translating Obj-C to MacRuby
John Shea
johnmacshea at gmail.com
Wed Oct 14 01:34:15 PDT 2009
Hi Bernd,
YES and NO in ObjC are translated to true and false in MacRuby.
so you were probably after:
NSNumber.numberWithBool(false)
(I am curious as to how often that is useful actually)
I will leave the table idea for others to comment - I actually think
in the end there are only a few rules to learn and then you will find
yourself translating easily (not that those rules should not go in a
cheat sheet somewhere - that's probably a good idea).
With:
NSDictionary.dictionaryWithObjectsAndKeys "a", "b", nil
I assume that in the original ObjC method is actually passing an array
which (it seems) must be terminated by a nil (in objC).
I actually ran across this in another context - passing objects in
this way - again I will leave others to comment on it, perhaps it is
an issue.
However why can't you use :
dict = {"b"=>"a"}
=> {"b"=>"a"}
#check class of created dictionary
dict.class
=> NSMutableDictionary
Why use the long winded ObjC form?
If you need a immutable NSDictionary for some reason:
dict = NSDictionary.alloc.initWithDictionary({"a"=>"b", "c" => "d"})
#or if you like typing
dict = NSDictionary.dictionaryWithObjects(["b","d"], forKeys:
["a","c"],count:1)
(and many other similar methods, I am sure) works fine.
Cheers,
John
On Oct 14, 2009, at 10:03 , 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.
>
> 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!
>
> 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).
>
> 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)
>
> Greets,
> Bernd
>
>
>
>
>
> _______________________________________________
> 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/20091014/4da3763e/attachment-0001.html>
More information about the MacRuby-devel
mailing list