Forum: IronRuby IronRuby for GIMP

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.
Maurits R. (Guest)
on 2008-10-27 17:44
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
Jim D. (Guest)
on 2008-10-30 05:38
(Received via mailing list)
Did anyone ever help you with this?
Tomas M. (Guest)
on 2008-10-30 06:24
(Received via mailing list)
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
Maurits R. (Guest)
on 2008-10-30 09:33
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
Michael L. (Guest)
on 2008-10-30 16:06
(Received via mailing list)
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. <
Maurits R. (Guest)
on 2008-10-30 16:45
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<Procedure> 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
Curt H. (Guest)
on 2008-10-30 17:13
(Received via mailing list)
"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.
Maurits R. (Guest)
on 2008-10-30 17:50
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:
<snip>
> 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
Curt H. (Guest)
on 2008-10-31 01:01
(Received via mailing list)
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)?
Ted M. (Guest)
on 2008-10-31 08:50
(Received via mailing list)
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?
Maurits R. (Guest)
on 2008-10-31 09:40
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
This topic is locked and can not be replied to.