Make a simple button wrapper class and put the logic from your Button
method in the constructor.

class Button
  def initialize()
     if gtk
       @button =3D GTK::Button
    ...
    end
  end

  def method_missing(...)
    @button.send(....)
  end
end

2011/12/8 Marc Heiler <shevegen / linuxmail.org>:
> Hi.
>
> I once had the idea of an abstracted UI language.
>
> My first attempt was to do something like:
>
> =A0Button.new
>
> And convert the Button to the specific toolkit in
> question - i.e. GTK Button, QT Button and so on.
>
> This was not really possible, at least not for me.
>
> When you have:
>
> =A0class Button
>
> Then ruby does not allow you to change that object
> lateron. In other words, it will always be of class
> Button, and never of class Gtk::Button, which I would
> need in order to make an abstracted toolkit.
>
> Lately I had another idea. I could simply try to
> trick Ruby.
>
> def Button
> =A0# Do something here, decide which toolkit to
> =A0# use, then return that.
>
>
> It would then allow me to do something like:
>
> =A0use :gtk
>
> =A0Button.new
>
> The interface logic would be described only once
> and then be valid no matter which toolkit would
> be used, within reason. (I am more interested in
> finding a common base within all toolkits, and
> using specialized solutions within that toolkit
> only lateron.)
>
> I could then switch to qt like this:
>
> =A0use :qt
>
>
>
> Ok, I tried a first proof of concept but failed.
>
> --------------------------------------
>
> require 'gtk2'
>
> $use_gtk =3D true
>
> def Button
> =A0if $use_gtk
> =A0 =A0return Gtk::Button
> =A0else # else use something else.
> =A0end
> end
>
> x =3D Button.new
> puts x.class
>
> --------------------------------------
>
> ^^^^^ The above code does not work. It stops with an
> error like this here:
>
> =A0uninitialized constant Button (NameError)
>
> It works however when I change the line to:
>
> =A0x =3D Button().new
>
> But this is ugly. I don't think I want to use that,
> it does not please my eyes.
>
>
> Is there a way to tell ruby to change its behaviour
> and treat Button not as a constant, but instead as
> a method invocation call where the () parens are
> omitted?
>
> I think if I would have that option, I could write a
> cross UI DSL "language".
>
> For any more ideas I am quite happy too. My ultimate
> goal is to actually use just ONE way to describe ALL
> User Interface elements, including the WWW. Be it
> in Ruby, or with an abstract interface language that
> has to be parsed, does not matter that much to me.
>
> Though using Ruby directly would simplify my life
> of course.
>
> --
> Posted via http://www.ruby-forum.com/.
>