• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Creating NSStrings in RubyCocoa
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Creating NSStrings in RubyCocoa


  • Subject: Creating NSStrings in RubyCocoa
  • From: Knud Hinnerk Möller <email@hidden>
  • Date: Thu, 17 Jan 2008 18:30:39 +0000

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("~/ Desktop").stringByExpandingTildeInPath

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?

Cheers,
Knud
-------------------------------------------------
Knud Möller, MA
+353 - 91 - 495086
Smile Group: http://smile.deri.ie
Digital Enterprise Research Institute
  National University of Ireland, Galway
Institiúid Taighde na Fiontraíochta Digití
  Ollscoil na hÉireann, Gaillimh


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Outline view selection doesn't move when items are inserted?
  • Next by Date: Re: Outline view selection doesn't change when new items are inserted?
  • Previous by thread: Re: Starting an alert sheet from an NSWindowController that owns a user-editable sheet
  • Next by thread: Show application when dock icon clicked
  • Index(es):
    • Date
    • Thread