IronRuby for GIMP


#1

I’m completely new at IronRuby and I try to use it for writing GIMP
plug-ins, using my GIMP# library
(http://sourceforge.net/projects/gimp-sharp/).

I have hacked up the following code:

require “…/…/lib/gimp-sharp”
require “mscorlib”

class RubySample < Gimp::Plugin

def initialize(args)
super(args, ‘RubySample’)
end

end

puts ARGV[0]
RubySample.new(ARGV)


But I get the following error: unknown: wrong number or type of
arguments for `initialize’ (ArgumentError)

The Plugin class has a constructor that takes two arguments: an array of
strings (the command line arguments) and a string. Does IronRuby have a
different notion of arrays and/or strings than the CLR? If so, how do I
convert them?

Thanks, Maurits


#2

Did anyone ever help you with this?


#3

RubyArrays are not implicitly convertible to CLR arrays.

You need something like:

def to_string_vector array
list = System::Collections::Generic::List[ClrString].new
array.each {|x| list.add(x.to_s.to_clr_string) }
list.to_array
end

Tomas


#4

Thanks!

I’m still a bit surprised that one has to do the conversion explicitly.
I would expect that either IronRuby uses CLR arrays (and strings) or
would do the conversion implicitly for me. But as I said in my previous
mail, I’m new to IronRuby and probably missing a whole bunch of design
decisions and other relevant information.

Regards, Maurits

Tomas M. wrote:

RubyArrays are not implicitly convertible to CLR arrays.

You need something like:

def to_string_vector array
list = System::Collections::Generic::List[ClrString].new
array.each {|x| list.add(x.to_s.to_clr_string) }
list.to_array
end

Tomas


#5

FWIW I’ve used this helper method:

class Array

def to_clr_arr
    arr = System::Array.CreateInstance self[0].class.to_clr_type,

self.length.to_clr_int
self.each_with_index { |r, i| arr[i] = r }
return arr
end
end

Here’s the to_clr_int method:

class Object
def to_clr_int
System::Int32.parse(self.to_s)
end
end

On Thu, Oct 30, 2008 at 12:10 AM, Tomas M. <


#6

Nice, the array conversion seems to work quite well. Right now I’m
encountering the next problem. The Plugin base class (C#) has the follow
signature:

protected abstract IEnumerator ListProcedures();

How can I implement this in my IronRuby derived class? Of course I tried
something like:

def ListProcedures
yield new Procedure(…)
end

However, this method never gets called.

Thanks,

Maurits


#7

“yield” doesn’t do in Ruby what it does in C#.

You’ll probably need to define your own enumerator class doing something
like this:

class ProcedureEnumerator
include System::Collections::Generic::IEnumerator.of(Procedure)

def initialize *proc_list
@proc_list = proc_list
@pos = 0
end

def MoveNext
@pos = @pos + 1 if @pos < @proc_list.length
throw :IndexError if @pos < @proc_list.length
end

def Reset
@pos = 0
end

def Current
@proc_list[@pos]
end
end

def ListProcedure
ProcedureEnumerator.new(Procedure.new(…))
end

Alternatively, you could create a statically-typed list of Procedure and
get an enumerator from that:

list = System::Collections::Generic::List.of(Procedure)
list.add(Procedure.new(…))

Finally, we’re shortly going to change the naming policy for overrides
of CLS virtual methods. When this happens, you’ll need to define
ProcedureEnumerator with the “rubified” method names: move_next, reset
and current.


#8

Do you know that your script is being loaded at all? How are you
integrating with the extension model (which no doubt expects an
assembly)?


#9

Hi Curt,

Curt H. wrote:

“yield” doesn’t do in Ruby what it does in C#.

Ah, I wasn’t aware of that.

You’ll probably need to define your own enumerator class doing something
like this:

def ListProcedure
ProcedureEnumerator.new(Procedure.new(…))
end

For some weird reason my method ListProcedure never gets called. At
first I thought it might have the wrong signature, so I tried to
override the simplest function in the Plugin base class (public virtual
void Init() {}), but even that didn’t work.

Investigations will continue and of course all the help already given in
this forum is very much appreciated!

Thanks,

Maurits


#10

Curt H. wrote:

Do you know that your script is being loaded at all? How are you

Yes, I’m very sure. The IronRuby class uses the constructor of the base
class, which has some print statements in it.

integrating with the extension model (which no doubt expects an
assembly)?

I use a require statement to load my gimp-sharp.dll. This seems to work
quite well because GIMP actually loads my IronRuby plug-in. Only problem
I still have is that it doesn’t overload the abstract method
ListProcedures, which means that the plug-in doesn’t register itself
yet.

Thanks,

Maurits


#11

On Thu, Oct 30, 2008 at 10:12 AM, Curt H. removed_email_address@domain.invalid
wrote:

Finally, we’re shortly going to change the naming policy for overrides of CLS virtual methods. When this happens, you’ll need to define ProcedureEnumerator with the “rubified” method names: move_next, reset and current.

I still do not like this decision. Any chance there could be a flag
we could set to get back to using the normal .NET naming?