Forum: Ruby question about method call

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.
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2008-10-25 23:40
Hi all,

I copy a working script from the website. I wonder if method "create"
defined in this class is ever called?


Thanks,

Li

#########script##############
require 'fox16'

include Fox

class HelloWindow < FXMainWindow
  def initialize(app)
    super(app, "Hello, World!", :width => 200, :height => 100)
  end

  def create
    super
    show(PLACEMENT_SCREEN)
  end
end

app = FXApp.new
HelloWindow.new(app)
app.create
app.run
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-27 14:10
(Received via mailing list)
2008/10/25 Li Chen <chen_li3@yahoo.com>:
> I copy a working script from the website. I wonder if method "create"
> defined in this class is ever called?

And, what did your tests show?

robert
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2008-10-27 14:37
Robert Klemme wrote:
> 2008/10/25 Li Chen <chen_li3@yahoo.com>:
>> I copy a working script from the website. I wonder if method "create"
>> defined in this class is ever called?
>
> And, what did your tests show?
>
> robert

It is working. But I am confused: why does app, an instance of
FXApp.new, receive a message defined by class by HelloWindow (or
FXMainWindow) and response it?

Li
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-27 16:48
(Received via mailing list)
2008/10/27 Li Chen <chen_li3@yahoo.com>:
> Robert Klemme wrote:
>> 2008/10/25 Li Chen <chen_li3@yahoo.com>:
>>> I copy a working script from the website. I wonder if method "create"
>>> defined in this class is ever called?
>>
>> And, what did your tests show?
>
> It is working. But I am confused: why does app, an instance of
> FXApp.new,

Read your code again.

> receive a message defined by class by HelloWindow (or
> FXMainWindow) and response it?

You probably need more coffee - or more sleep. :-)

robert
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2008-10-27 18:32
Robert Klemme wrote:
> Read your code again.
>
>> receive a message defined by class by HelloWindow (or
>> FXMainWindow) and response it?
>
> You probably need more coffee - or more sleep. :-)
>
> robert


Reading from the codes  it looks  app receive a message from
FXApp.new, which I have no problem to understand that. But if I remove
"create"  defined by  class HelloWindow(or FXMainWindow) the script
won't work. This is why I am confused.

Li
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-27 19:00
(Received via mailing list)
On 27.10.2008 18:32, Li Chen wrote:
> "create"  defined by  class HelloWindow(or FXMainWindow) the script
> won't work. This is why I am confused.

You are right: I am the one who needs more coffee or sleep. :-}  I do
not know Fox but I would guess that the FXMainWindow#initialize
registers with FXApp instance and that in turn sends messages it does
not understand to the main window instance.  Not difficult to do:

irb(main):001:0> class App
irb(main):002:1> def register(obj)
irb(main):003:2> @delegat = obj
irb(main):004:2> end
irb(main):005:1> def method_missing(*a,&b)
irb(main):006:2> @delegat ? @delegat.send(*a,&b) : super
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> class Win
irb(main):010:1> def initialize(app) app.register(self) end
irb(main):011:1> def boo() puts "boo!" end
irb(main):012:1> end
=> nil
irb(main):013:0> a = App.new
=> #<App:0x7ff64ddc>
irb(main):014:0> w = Win.new a
=> #<Win:0x7ff62438>
irb(main):015:0> a.boo
boo!
=> nil
irb(main):016:0>

Cheers

  robert
73700e119917433681f2e8f3e4369f74?d=identicon&s=25 Li CN (alex-osu3)
on 2008-10-30 17:41
Hi Robert,

I post my question at fxruby-users@rubyforge.org. Based on the feedback
and what I understand  I explain it as follows:

I add some lines to find out 1) object and its corresponding class
2) class and its corresponding superclass(up to final superclass).

Here are what I find:
app object is created by class FXApp and its parent class is FXObject.
hello object is created by class HelloWindow and its parent class is
FXMainWindow.
The final parent for class HelloWindow  is FXObject(by repeating calling
method superclass 6-7 times. I call all the parents in this chain as one
of parent chain). And class FXApp never appears in the chain parernt.

Here is my conclusion:
1) the relationship between class FXApp and  HelloWindow/FXMainWindow is
horizontal or brother/sister. Their final parent is FXObject(whose final
parent should be Object.) The point here is that in line
app.create(),app objcet REALLY receives #create() defined by its
corresponding class FXApp but not by #create() defined by class
HelloWindow/FXMainWindow. There is no inherence issues between class
FXApp and class HelloWindow/FXMainWindow.

2) But what really happens after app.create() is excuted: it causes
#create() defined by each widget to be called(HelloWindow widget in this
case). This is why class HelloWindow/FXMainWindow has to define
#create() in its class. This might be called window hierarchy but it is
NOT class inherence in Ruby. If I understand correctly it looks like it
is a design  strategy for FxRuby: client object and server  resource...
And this is what Lyle mentions how Rxruby works on page 90 in chapter 7.


Li



if __FILE__ == $0

FXApp.new do |app|

  puts "app belongs to class: #{app.class}"

     puts "FXApp has superclasss: #{FXApp.superclass}"

  puts

    hello=HelloWindow.new(app)

  puts "hello belongs to class: #{hello.class}"

  puts "HelloWindow has superclasss: #{HelloWindow.superclass}"

  app.create

       app.run

  end

end
This topic is locked and can not be replied to.