On 9/7/07, Wilson Bilkovich <wilsonb / gmail.com> wrote:
> On 9/7/07, Marc Heiler <shevegen / linuxmail.org> wrote:
> > How can I convert my object "foobar" of "class Button"
> >
> >   foobar = Button.new
> >
> > to become a "real" Gtk::Button for instance?
> >
>
> foobar = Gtk::Button.new


This only binds the VARIABLE foobar in the current scope to a
different object, it DOESN'T change the OBJECT which foobar pointed to
originally.  So for example

def dub_gtk_button(button)
    button = Gtk::Button.new
   puts "in dub_gtk #{button.class}"
end

button = Button.new
puts "before #{button.class}"
dub_gtk(button)
puts "before #{button.class}"

will print:

before Button
in dub_gtk Gtk::Button
after Button

If you want to change defer determining the behavior of a ruby  object
until sometime after it's instantiated, there are better ways to do it
than changing the class.

1) As others have pointed out you can have the original object
delegate to a reall implementation obuect perhaps using delegator or
forwardable from the standard library.

2) You can treat the original class of the object as a superclass, and
add the particular behavior either by adding singleton methods, or by
mixing one or more modules into the instance using the Object#extend
method.

The second approach might seem cooler and might perform slightly
better although if you want to change the behavior more than once it
can more be problematical than when using the first.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/