Forum: IronRuby How to instanciate a System::String[] from IronRuby?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (thbar)
on 2009-04-02 17:00
(Received via mailing list)
Hi,

while trying to call some C# code from IronRuby, I get this:

my_csharp_class_instance.call(%w(some array))

:0: can't convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a
conversion ?

cheers,

-- Thibaut
F983f0c990cba2fe743ef62a975ec99c?d=identicon&s=25 Curt Hagenlocher (Guest)
on 2009-04-02 17:28
(Received via mailing list)
I don't know if it's the easiest way, but this should work as a
conversion:

x = ['a', 'b', 'c']
System::Array[System::String].new(x.map { |s| s.to_s.to_clr_string })

(Obviously, if you know that the elements are already Ruby strings, you
can omit the "to_s".)

From: ironruby-core-bounces@rubyforge.org
[mailto:ironruby-core-bounces@rubyforge.org] On Behalf Of Thibaut
Barrère
Sent: Thursday, April 02, 2009 7:36 AM
To: ironruby-core
Subject: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my_csharp_class_instance.call(%w(some array))

:0: can't convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a
conversion ?

cheers,

-- Thibaut
68f1bd9e9a7d9d10f0278c6535f700fe?d=identicon&s=25 Michael Letterle (mletterle)
on 2009-04-02 17:28
(Received via mailing list)
class Array
        def to_clr_arr
        arr = System::Array.CreateInstance self[0].class.to_clr_type,
System::Int32.pase(self.length.to_s)
        self.each_with_index { |r, i| arr[i] = r }
        return arr
    end
end

my_csharp_class_instance.call(%w(some array).map{|x|
x.to_clr_string}.to_clr_arr)

2009/4/2 Thibaut Barrère <thibaut.barrere@gmail.com>
Ade8632553a9243ae05fc920f68644c1?d=identicon&s=25 Jim Deville (Guest)
on 2009-04-02 17:43
(Received via mailing list)
Jimmy and I have discussed a to_clr_array(type) where type defaults to
Object, but nothing like that exists yet.


JD
________________________________
From: ironruby-core-bounces@rubyforge.org
[ironruby-core-bounces@rubyforge.org] on behalf of Curt Hagenlocher
[curth@microsoft.com]
Sent: Thursday, April 02, 2009 8:03 AM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?

I don’t know if it’s the easiest way, but this should work as a
conversion:

x = ['a', 'b', 'c']
System::Array[System::String].new(x.map { |s| s.to_s.to_clr_string })

(Obviously, if you know that the elements are already Ruby strings, you
can omit the “to_s”.)

From: ironruby-core-bounces@rubyforge.org
[mailto:ironruby-core-bounces@rubyforge.org] On Behalf Of Thibaut
Barrère
Sent: Thursday, April 02, 2009 7:36 AM
To: ironruby-core
Subject: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my_csharp_class_instance.call(%w(some array))

:0: can't convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a
conversion ?

cheers,

-- Thibaut
Cb51033949ffccd982ae32c9f890f25a?d=identicon&s=25 Tomas Matousek (Guest)
on 2009-04-02 20:40
(Received via mailing list)
This is the right way:

x = ['a', 'b', 'c']
System::Array[System::String].new(x.map { |s| s.to_s.to_clr_string })

This would simplify to

x = ['a', 'b', 'c']
System::Array[System::String].new(x)

As soon as we implement implicit conversions here.

Tomas

From: ironruby-core-bounces@rubyforge.org
[mailto:ironruby-core-bounces@rubyforge.org] On Behalf Of Jim Deville
Sent: Thursday, April 02, 2009 8:41 AM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?

Jimmy and I have discussed a to_clr_array(type) where type defaults to
Object, but nothing like that exists yet.


JD
________________________________
From: ironruby-core-bounces@rubyforge.org
[ironruby-core-bounces@rubyforge.org] on behalf of Curt Hagenlocher
[curth@microsoft.com]
Sent: Thursday, April 02, 2009 8:03 AM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?
I don't know if it's the easiest way, but this should work as a
conversion:

x = ['a', 'b', 'c']
System::Array[System::String].new(x.map { |s| s.to_s.to_clr_string })

(Obviously, if you know that the elements are already Ruby strings, you
can omit the "to_s".)

From: ironruby-core-bounces@rubyforge.org
[mailto:ironruby-core-bounces@rubyforge.org] On Behalf Of Thibaut
Barrère
Sent: Thursday, April 02, 2009 7:36 AM
To: ironruby-core
Subject: [Ironruby-core] How to instanciate a System::String[] from
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my_csharp_class_instance.call(%w(some array))

:0: can't convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a
conversion ?

cheers,

-- Thibaut
91eb330fb36d1e03c856574dfb77d2bc?d=identicon&s=25 Thibaut Barrère (thbar)
on 2009-04-03 01:43
(Received via mailing list)
Hi,
thanks for the feedback - I'll give these a go.

cheers

-- Thibaut
This topic is locked and can not be replied to.