[MacRuby-devel] When is a String not an NSString?

Laurent Sansonetti lsansonetti at apple.com
Tue Jul 13 13:02:18 PDT 2010


Hi,

I believe this has been discussed on trac already, although I can't seem to find the ticket.

In short, this is expected behavior. A method added to the String class will only be available to strings created from Ruby. If you want a method to be available to all strings, add it to the NSString class. I do agree that this can be confusing behavior and maybe we can try to reduce the confusion somehow. This design was implemented in 0.6 and is the result of several debates. I personally didn't expect people would notice this :)

Laurent

On Jul 13, 2010, at 10:59 AM, Michael Jackson wrote:

> This sounds like a bug to me. Have you filed a ticket?
> 
> http://trac.macosforge.org/projects/ruby/newticket
> 
> --
> Michael Jackson
> http://mjijackson.com
> @mjijackson
> 
> 
> 
> On Mon, Jul 12, 2010 at 6:02 PM, Henry Maddocks
> <henry.maddocks at gmail.com> wrote:
>> With respect to the following code
>> 
>>     1  require 'test/unit'
>>     2
>>     3  class String
>>     4      def my_method
>>     5          "My Method " + self
>>     6      end
>>     7  end
>>     8
>>     9  class Test_Tumblr < Test::Unit::TestCase
>>    10
>>    11    def test_strings
>>    12      cocoa_string = NSString::stringWithString("blah")
>>    13      ruby_string = "Ruby String"
>>    14
>>    15      assert_equal String, cocoa_string.class
>>    16      assert_equal String, ruby_string.class
>>    17      assert_equal cocoa_string.class, ruby_string.class
>>    18
>>    19      assert ruby_string.respond_to?(:my_method), "ruby_string.respond_to?"
>>    20      assert cocoa_string.respond_to?(:my_method), "cocoa_string.respond_to?"
>>    21    end
>>    22
>>    23  end
>> 
>> 
>> What is the expected result of line 20?
>> 
>> On the one hand cocoa_string is an NSString so the monkey patch shouldn't have any effect, but on the other cocoa_string is claiming to be a ruby String.
>> 
>> Henry
>> _______________________________________________
>> MacRuby-devel mailing list
>> MacRuby-devel at lists.macosforge.org
>> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel



More information about the MacRuby-devel mailing list