Reputation: 61
Im developing a really simple application in Ruby and decided to use Shoes Gui, im trying to use the new fourth version but i cannot call shoes objects outside the app, for example in the Shoes Manual http://shoesrb.com/manual/Rules.html this code doesn't work:
class Messenger
def initialize(stack)
@stack = stack
end
def add(msg)
@stack.append do
para msg
end
end
end
when i pass the stack object from the app nothing happens, thanks for your help
Upvotes: 2
Views: 152
Reputation: 155
First of all, change class Messenger
to class Messenger < Shoes
. This is essential for any and all classes in Shoes. Then, put the Shoes.app
statement after the end
statement of the class, but don't add the do
. Your code should look like this:
class Messenger < Shoes
# Your code goes here...
end
Shoes.app [styling goes here]
NOTE: Put the Shoes.app
only once, at the end, after all classes have been defined.
Upvotes: 0
Reputation: 623
I had the same issue described here, but with the right piece of code from the manual. I have found something, so I'm posting it here, hoping it'll help someone (or that someone will explain me what I've not found).
Here is what I had first (it doesn't work, no matter where you put your class instantiation).
class Messenger
def initialize(stack)
@stack = stack
end
def add(msg)
@stack.app do
@stack.append do
para msg
end
end
end
end
Shoes.app do
stack margin: 20, width: 20 do
subtitle "Shoes box"
para "Maaamaaaaa"
button "Mama ?" do
s = Messenger.new(@box)
s.add("Yeah, mama !")
end
end
@box = stack
end
First thing : if you got these two pieces of code in the same file, you could do that :
class Messenger
...
@stack.app do
@box.append do
para msg
end
...
end
It works.
It doesn't work if you remove the @stack.app do
block or if you move this class in another file though. If you put your class instantiation somewhere else, it doesn't work either.
More interesting, now, and working even if you put your Messenger class somewhere else, you can do that :
class Messenger
...
def add(msg)
@stack.app do
para msg
end
...
Shoes.app do
...
button "Mama ?" do
@box.append do
s = Messenger.new(@box)
s.add("Yeah, mama !")
end
end
...
end
Now you also can move your class instantiation anywhere in your app block... but it still has to be BEFORE the box you're trying to append to, and that same box has to be declared BEFORE the button which is trying to add to it. Making the thing a bit tricky. Otherwise, you can let your class instantiation here. It means you will create a new object every time you're trying to append something to your box. And it is ugly. But it works.
Edit : just saw that, you can replace the @stack.append
do by self.append do
in the Messenger class. Though it doesn't really append anything, since it's adding the msg at the begining of your window.
Upvotes: 0
Reputation: 1624
I think you just missed out reading the manual completely. The manual very clearly says that this could apparently should work but it doesn't since the App object isn't around any more. Fortunately, each shoes object has a app method, so: You should replace your code with:
class Messenger
def initialize(stack)
@stack = stack
end
def add(msg)
@stack.app do
@stack.append do
para msg
end
end
end
end
Hope it helps :)
Upvotes: 0