Another go at getting to the heart of the problem.

On Wed, 20 Aug 2008, Jean Lazarou wrote:

Thank you all for answering, but if I go for simplicity nothing works...
        [...]
If I define a Shoes class as following and some methods (like animate):

13 class Shoes
14
15    def self.app params
16      yield
17    end
18
19
20 end
21

You then define these outside of Shoes, in whatever the main namespace
is.  In ruby that is Object.  And it is in Shoes as well.  How do I
know that? See below.

22  def animate fps, &block
23    yield
24  end
25
26  def clear
27    yield
28  end
29
30  def stack
31    yield
32  end


So where do you need to define these methods that you want to run inside that main Shoes.app block?

#!shoes
#
# Program to show what class Shoes.app produces.
#

$outside = self

Shoes.app do
  para( self.inspect );
  para("  ")
  para( self.class.to_s );
  para("  ")
  para($outside.class.to_s);
end


.inspect and .to_s are ways to turn ruby objects into strings, so we can print them.
On my system this produces:

(Shoes::App "Shoes")  Shoes::App  Object


So inside the Shoes.app block, self (the current object to which methods
are applied is an instance of Shoes::App.  So you need to define your
animate methods in

class Shoes::App
  ...
end


How do I know that is a class, and not a module? I changed my code to this:

#!shoes
#
# Program to show what class Shoes.app produces.
#

$outside = self

Shoes.app do
  para( self.inspect );
  para("  ")
  para( self.class.to_s );
  para(" which is a ")
  para( self.class.class.to_s );
  para("  ")
  para($outside.class.to_s);
end


So the class of self.class is Class, rather than Module.
I get

test.rb:38: undefined local variable or method `draw_background' for
#<Object:0x288fa34 @time=Wed Aug 20 19:56:46 +0200 2008> (NameError)
    from test.rb:27:in `clear'

this is because you defined it outside of everything, so it is a method of Object.


I don't find any easy way to make the code run. Of course, if I comment
the call to `draw_background' (line 38) I get the output => Time = Wed
Aug 20 20:03:22 +0200 2008

(I run the code with the C-Ruby runtime)

Is there some Ruby technical I don't know?

Jean

        Hugh

Reply via email to