Ruby Gtk, classe inheritance and initialize method problem

Hi,

I just want to create a subclass of Gtk::Windows and set parameters for
the initialize method of this subclass. Here is the simple code:

#!/usr/bin/ruby
require ‘gtk2’

class RubyApp < Gtk::Window
def initialize( a , b)
super
signal_connect “destroy” do
Gtk.main_quit
end
@columns = a
@rows = b
init_table(@columns,@rows)
show_all
end
def init_table (columns, rows)
table = Gtk::Table.new(columns,rows)
my_button=Array.new
for a in 0…3
my_button[a] =Gtk::Button.new(“boite numero :”+a.to_s)
table.attach(my_button[a],0,4,a,a+1)
end
add table
end
end

Gtk.init
window = RubyApp.new(4, 5)
Gtk.main

If i run the script, I get this error:
in `initialize’: wrong number of arguments (2 for 1) (ArgumentError)

I try a lot of things and after a lot of research on the net, I can’t
find the solution or the tricks that should solve the problem.

Have you got any idea why I can’t define new parameters for the
initialize method.

Thanks

Oh yes this really helps me,

that solve my problem

Thanks a lot Jesus

On Fri, May 6, 2011 at 5:52 PM, Silkmoth S. [email protected] wrote:

super
my_button=Array.new
Gtk.main
Thanks


Posted via http://www.ruby-forum.com/.

If I’m not mistaken, you are using this library:

When you call super without params, it calls the same method you are
in (in this case initialize), with the same params you received. So
you are calling the Gtk::Window#initialize passing the two parameters
you passed to your initialize method. As you can see, Gtk::Window
doesn’t override initialize, it inherits the method from a parent
class. From the error message, you see it’s expecting only one
parameter (from the method Gtk::Window.new we might think that the
parameter is the window type), and you are passing two, hence the
error. You can do like this:

class RubyApp < Gtk::Window
def initialize( a , b)
super() # I think the parameter is optional, looking at the
Gtk::Window.new documentation

Hope this helps,

Jesus.