From: Gunther Diemant Date: 2011-12-08T17:28:17+09:00 Subject: Re: Using any trick to omit () in constant lookup? Make a simple button wrapper class and put the logic from your Button method in the constructor. class Button def initialize() if gtk @button = GTK::Button ... end end def method_missing(...) @button.send(....) end end 2011/12/8 Marc Heiler : > Hi. > > I once had the idea of an abstracted UI language. > > My first attempt was to do something like: > >  Button.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: > >  class 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 >  # Do something here, decide which toolkit to >  # use, then return that. > > > It would then allow me to do something like: > >  use :gtk > >  Button.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: > >  use :qt > > > > Ok, I tried a first proof of concept but failed. > > -------------------------------------- > > require 'gtk2' > > $use_gtk = true > > def Button >  if $use_gtk >    return Gtk::Button >  else # else use something else. >  end > end > > x = Button.new > puts x.class > > -------------------------------------- > > ^^^^^ The above code does not work. It stops with an > error like this here: > >  uninitialized constant Button (NameError) > > It works however when I change the line to: > >  x = 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/. >