我正在尝试扩展我已经编写的 Ruby 应用程序以使用 Shoes。我已经编写了一个类,并且希望能够在该类中使用 GUI。也就是说,我希望我的班级有这样的内容:
class MyClass
def draw
# draw something using Shoes
end
end
里面还有一个方法MyClass
将会通知draw()
当它想画东西的时候。
我尝试了多种方法,但似乎都不起作用。我可以将整个课程封装在一个 Shoes 应用程序中。假设我想画一个椭圆形:
Shoes.app {
class MyClass
def draw
oval :top => 100, :left => 100, :radius => 30
end
end
}
但随后它说undefined method 'oval' for MyClass
.
我也尝试过这个:
class MyClass
def draw
Shoes.app {
oval :top => 100, :left => 100, :radius => 30
}
end
end
运行成功,但每次都会打开一个新窗口test()
叫做。
如何从实例方法内部使用 Shoes 绘制东西?
Shoes.app { ... }
执行代码块的instance_eval。这意味着块的主体被执行,就像 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)