Projects

Ticket #726 (new defect)

Opened 21 months ago

Last modified 14 months ago

Methods added to String do not get added to all strings

Reported by: dylan@… Owned by: lsansonetti@…
Priority: blocker Milestone: MacRuby Later
Component: MacRuby Keywords:
Cc:

Description

Methods added to the string class are not being added to the string returned from NSMutableString. I've also seen this with stringValue from NSTextFieldCell.

The following will generate an error, even though the classes of the two objects are both 'String'.

I'm not sure if this occurs with other open classes.

class String
  def cook
    puts 'Bacon!'
  end
	
  def to_mutable_attributed_string
    NSMutableAttributedString.alloc.initWithString( self )
  end
end

base_string = 'breakfast'

puts base_string.class
base_string.cook

mutable = base_string.to_mutable_attributed_string

puts mutable.string.class
mutable.string.cook
	

Attachments

test.rb Download (397 bytes) - added by dylan@… 21 months ago.
Failing Test

Change History

Changed 21 months ago by dylan@…

Failing Test

Changed 21 months ago by martinlagardette@…

  • milestone MacRuby 0.6 deleted

Hi!

This is because of the way strings are implemented within MacRuby. If you look at the result of this command:

macruby -e 'p String.ancestors'
[String, NSMutableString, NSString, Comparable, NSObject, Kernel]

You can see that String is defined on top of of NSMutableString, which is why defining a method on String won't define it on NSMutableString or NSString.

In your example, if you replace "class String" by "class NSString", then it will work as expected:

String
Bacon!
String
Bacon!

Changed 21 months ago by dylan@…

The fix works. Thanks!

Still, the ancestor list is the same for the string I create and the one coming back from the mutable string (they're both [String, NSMutableString, NSString, Comparable, NSObject, Kernel]). Is this not just masquerading as a String when it comes back and is really and instance of NSMutableString or NSString ?

Changed 21 months ago by martinlagardette@…

Yes, there is some kind of "masquerading", because we want Strings to be NSStrings, and vice-versa, this is how you can send Strings to Obj-C APIs and NSString to ruby APIs :-)

Changed 14 months ago by lsansonetti@…

  • milestone set to MacRuby Later

I don't think we will change this in 1.0, screening for Later.

Note: See TracTickets for help on using tickets.