Search code examples
rubyshoes

Making UI communicate with logic


I have begun playing around with Shoes.rb and I have an hard time making the UI working with some logics. For instance I'd like to send a message to an object when a button is clicked so I have the following code:

    Shoes.app do
      button "Run" do
        @label.replace Calculator.add(1,1)
      end
      @label = para "Result will show up here"
   end

I have also my Calculator class

class Calculator
  def self.add(x,y)
    x+y
  end
end

How can I make it work? I have tried to add the class under the Shoes block (adding also it into a module) and having the class in a separate required file but nothing works.

Thanks.


Solution

  • Put Calculator definition before the Shoes.app block:

    class Calculator
      def self.add(x,y)
        x+y
      end
    end
    
    Shoes.app do
      button "Run" do
        @label.replace Calculator.add(1,1)
      end
      @label = para "Result will show up here"
    end
    

    Otherwise, code in the Shoes.app block can't access the Calculator class.

    before click

    after click