[MacRuby] #726: Methods added to String do not get added to all strings
#726: Methods added to String do not get added to all strings ---------------------------------+------------------------------------------ Reporter: dylan@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: MacRuby 0.6 Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ 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 }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/726> MacRuby <http://macruby.org/>
#726: Methods added to String do not get added to all strings ---------------------------------+------------------------------------------ Reporter: dylan@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Changes (by martinlagardette@…): * milestone: MacRuby 0.6 => Comment: 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! }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/726#comment:1> MacRuby <http://macruby.org/>
#726: Methods added to String do not get added to all strings ---------------------------------+------------------------------------------ Reporter: dylan@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(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 ? -- Ticket URL: <http://www.macruby.org/trac/ticket/726#comment:2> MacRuby <http://macruby.org/>
#726: Methods added to String do not get added to all strings ---------------------------------+------------------------------------------ Reporter: dylan@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: ---------------------------------+------------------------------------------ Comment(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 :-) -- Ticket URL: <http://www.macruby.org/trac/ticket/726#comment:3> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby