A string is a string is a ... well, at least not string.
C:\ironruby\bin>ir IronRuby 1.0.0.2 on .NET 2.0.50727.3053 >>> require 'Guestbook' => true >>> @repo = Guestbook::MessageRepository.new "connstring" => #<Guestbook::MessageRepository:0x000005c> >>> @str = @repo.ConnectionString => "connstring" >>> @anotherstr = "connstring" => "connstring" >>> @str == @anotherstr => false
Huh? "connstring" != "connstring"? This bit me quite hard, but the following helped me resolve the problem:
>>> @str.class => ClrString >>> @anotherstr.class => String
So, when you instantiate a string from a CLR object (Guestbook above is a .NET dll), you don't get a ruby String, but a ClrString. When you use the ClrString, e.g. when printing it out to the console, the to_s method is automatically invoked, and it looks to the ir user as if it is the same as the ruby String created when invoking to_s. Sneaky.
If I had been using IronRuby 0.3.0 instead, I wouldn't have been bitten by this:
C:\ironruby-0.3.0\bin>ir IronRuby 0.3.0.0 on .NET 2.0.50727.3053 Copyright (c) Microsoft Corporation. All rights reserved. >>> require 'Guestbook' => true >>> @repo = Guestbook::MessageRepository.new "connstr" => #<Guestbook::MessageRepository:0x000005c> >>> @str = @repo.ConnectionString => 'connstr' >>> @str.class => ClrString >>> @anotherstr = "connstr" => "connstr" >>> @str => 'connstr' >>> @anotherstr => "connstr" >>> @str == @anotherstr => false
When printing out the ClrString @str to the console, it's quoted with single quotes. Smart.
How does this work with JRuby-Java Interop?
0 kommentarer:
Post a Comment