Creating NSStrings in RubyCocoa

  • Hi,

    I'm new to RubyCocoa, so this question may seem dumb. I know that, for
    some of the fundamental types, RubyCocoa will convert instances of
    ruby classes into instances of the corresponding cocoa classes when
    you pass them to an Obj-C method. E.g., a ruby string would be
    converted into an NSString. However, what is the proper way of
    generating an NSString (or similar 'basic' objecs) from scratch? I had
    this code:

        desktopPath = "~/Desktop".stringByExpandingTildeInPath

    This gave me an exception, complaining that
    stringByExpandingTildeInPath is not a method of class "String":

        undefined method `stringByExpandingTildeInPath' for "~/
    Desktop":String (NoMethodError)

    So, apparently, I had created a ruby string and not an NSString. After
    some searching, I found that there is supposedly a method "to_ns" that
    will convert ruby objects into corresponding cocoa objects. However,

        desktopPath = "~/Desktop".to_ns.stringByExpandingTildeInPath

    just gave this error:

        undefined method `to_ns' for "~/Desktop":String (NoMethodError)

    The only solution I found was using the cocoa constructor methods for
    explicitly creating an NSString:

        desktopPath = OSX::NSString.alloc.initWithString("~/

    This, however, looks rather ugly and complicated, especially for
    something as simple as creating a string object. Is there a better
    solution to doing this?

    Knud Möller, MA
    +353 - 91 - 495086
    Smile Group:
    Digital Enterprise Research Institute
      National University of Ireland, Galway
    Institiúid Taighde na Fiontraíochta Digití
      Ollscoil na hÉireann, Gaillimh
previous month january 2008 next month
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today