[Ironruby-core] WinForms event handling

Jimmy Schementi Jimmy.Schementi at microsoft.com
Tue Apr 21 13:46:46 EDT 2009


In the latest builds this will work:

>>> s = System::String.new 'fubar'
=> 'fubar'
>>> s.class
=> System::String

Note that the result is single-quoted; that's a little hint that it's a System::String rather than a Ruby String. The reason they are different is mutability; Ruby strings are mutable, while the CLR's are not. Unfortunately this means that not all the methods on a Ruby string can work on a CLR string (like sub!, slice!, etc), but we have added any non-mutable methods to CLR strings:

>>> s.upcase
=> "FUBAR"

And any mutable methods will throw:

>>> s.upcase!
:0: Mutating method `upcase!' called for an immutable string (System::String) (TypeError)

> -----Original Message-----
> From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-
> bounces at rubyforge.org] On Behalf Of Andrew S. Townley
> Sent: Tuesday, April 21, 2009 10:08 AM
> To: ironruby-core at rubyforge.org
> Subject: [Ironruby-core] WinForms event handling
>
> Hi,
>
> I was doing a bit of experimenting with ironruby today to see if it
> might be useful for some prototyping work that I'm doing.  I noticed
> that any of the strings returned from the CLR don't automatically
> include the ruby string methods.  I would've expected that these would
> be mapped directly so that you could call @textfield.text.strip instead
> of @textfield.text.trim.
>
> Probably low on the priority list, but was just curious what the plan
> was.  Assignment to locally created string references doesn't help
> either.  I know it's early days yet, but this sort of thing will make
> it
> difficult for people to remember which API applies where--especially if
> CLR strings "escape" a particular method context.
>
> Interesting things seem to also happen if you try and create a CLR type
> directly:
>
> C:\>ir -v
> IronRuby 0.3 0.3.0.0 on .NET 2.0.0.0
>
> C:\>iirb
> irb(main):001:0> require 'mscorlib'
> => true
> irb(main):002:0> s = System::String.new "fubar"
> TypeError: can't convert String into System::Char*
>         from (irb):0
>         from :0:in `eval'
>         from workspace.rb:80:in `evaluate'
>         from context.rb:217:in `evaluate'
>         from irb.rb:147:in `eval_input'
>         from irb.rb:253:in `signal_status'
>         from irb.rb:146:in `eval_input'
>         from ruby-lex.rb:230:in `each_top_level_statement'
>         from :0:in `loop'
>         from ruby-lex.rb:229:in `each_top_level_statement'
>         from :0:in `catch'
>         from ruby-lex.rb:227:in `each_top_level_statement'
>         from irb.rb:102:in `eval_input'
>         from irb.rb:69:in `start'
>         from :0:in `catch'
>         from irb.rb:51:in `start'
>         from iirb:0irb(main):003:0>
>
> The only way that I've found to ensure that it's really a Ruby string
> is
> to do something like this
>
> s = "" << clrstring
>
> Has anyone else hit this yet?
>
> Cheers,
>
> ast
> --
> Andrew S. Townley <ast at atownley.org>
> http://atownley.org
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core



More information about the Ironruby-core mailing list