Ruby 如何从实例方法内部调用Shoes方法?

Ruby 如何从实例方法内部调用Shoes方法?,ruby,shoes,Ruby,Shoes,我正在尝试扩展一个Ruby应用程序,我已经为使用Shoes编写了这个应用程序。我有一个我已经编写的类,我希望能够将GUI与该类一起使用。也就是说,我希望我的班级有这样的东西: class MyClass def draw # draw something using Shoes end end MyClass中的另一个方法在需要绘制某些内容时将调用draw() 我试过几种方法,但似乎都不管用。我可以用一个鞋类应用程序包装整个班级。假设我想画一个椭圆形: Shoes.app {

我正在尝试扩展一个Ruby应用程序,我已经为使用Shoes编写了这个应用程序。我有一个我已经编写的类,我希望能够将GUI与该类一起使用。也就是说,我希望我的班级有这样的东西:

class MyClass
   def draw
    # draw something using Shoes
  end
end
MyClass
中的另一个方法在需要绘制某些内容时将调用
draw()

我试过几种方法,但似乎都不管用。我可以用一个鞋类应用程序包装整个班级。假设我想画一个椭圆形:

Shoes.app {
  class MyClass
    def draw
      oval :top => 100, :left => 100, :radius => 30
    end
  end
}
但随后它为MyClass显示了
未定义的方法“oval”

我也试过:

class MyClass
  def draw
    Shoes.app {
      oval :top => 100, :left => 100, :radius => 30
    }
  end
end
这将成功运行,但每次调用
test()
时都会打开一个新窗口


如何从实例方法内部使用Shoes绘制东西?

您可以做的是将GUI与绘图分开。每次打开新窗口的原因是每次调用draw方法时都会调用Shoes.app

试试这个:

class MyClass
  def draw
    oval :top => 100, :left => 100, :radius => 30
  end
  def test
    draw
  end
end

Shoes.app do
  myclass = MyClass.new
  myclass.test
end

Shoes.app{…}
执行代码块的实例评估。这意味着块体的执行就像self是
Shoes
的实例一样(或者它在引擎盖下使用的任何类)。您将要执行以下操作:

class MyClass
  def initialize(app)
    @app = app
  end
  def draw
    @app.oval :top => 100, :left => 100, :radius => 30
  end
end

Shoes.app {
  myclass = MyClass.new(self) # passing in the app here
  myclass.draw
}

那个代码不起作用。它说
参数数目错误(0代表1)
,尽管我不明白为什么。