Shurikan117
Shurikan117

Reputation: 105

Fetch text string for Shoes edit box from another file

My app runs on Shoes 3 and consists of the following code

require 'somefile'

Shoes.app do
  stack do
    flow do
      @my_editbox = edit_line
    end
    flow do 
      button "Get Name" do
        @my_editbox.text = "#{@name}"
      end
    end
  end
end

With my external file somefile.rb holding

@name = "my name"

Clicking the button does nothing, and my edit box stays empty. Any help is appreciated!

Upvotes: 0

Views: 60

Answers (1)

anothermh
anothermh

Reputation: 10574

That's not how Shoes works. Shoes is not Ruby, it only looks like Ruby. Many things that you know to work in Ruby simply will not work in Shoes, because Shoes is a toolkit written in C that will work in Ruby-ish ways by directly calling the Ruby APIs.

require calls are one of the things that will not work the way you expect. There is a nicely confusing explanation about some of these rules available on the Shoes website.

Personally, I found Shoes to be so frustrating and poorly documented that it wasn't worth using even in the extremely limited ways it can be used. Good luck.

Update

You asked below about the "how". I assume you mean, how do you properly use require in a Shoes app to load code from a separate file.

Take a look at this repo for an example. You can build a normal Ruby class and then require that class in your app. You can use that class within your Shoes.app do block in a normal Ruby way. But (as far as I can tell) because of the way self changes within the block, you cannot pull in a standalone instance variable that exists outside of a class/module.

You can do something like this, though, and it does work in the way you expect:

# foo.rb
module Foo
  @@name = 'foobar'
end

and

# test.rb
require './foo.rb'

Shoes.app do
  stack do
    flow do
      @my_editbox = edit_line
    end
    flow do 
      button "Get Name" do
        @my_editbox.text = Foo.class_variable_get(:@@name)
      end
    end
  end
end

Here I've created a module with a class variable because it doesn't make sense to use an instance variable in something that doesn't get instantiated.

There are certainly other ways of doing this as well, and you can probably find other examples on GitHub (though you may have to tweak that query to get more results), but this is a functional example to accomplish the task you've outlined.

Upvotes: 1

Related Questions