1

So I'm making a small program in VisualRuby that can tweet. So here's what my main.rb looks like:

#!/usr/bin/ruby

require 'vrlib'
require 'twitter_oauth'

#make program output in real time so errors visible in VR.
STDOUT.sync = true
STDERR.sync = true

#everything in these directories will be included
my_path = File.expand_path(File.dirname(__FILE__))
require_all Dir.glob(my_path + "/bin/**/*.rb") 

LoginWindow.new.show

and my LoginWindow.rb looks like this

require 'twitter_oauth'

class LoginWindow #(change name)

    include GladeGUI

    client = TwitterOAuth::Client.new(
        :consumer_key => '****',
        :consumer_secret => '****',
        :token => '****-****',
        :secret => '****'
    )

    def show()
        load_glade(__FILE__)  #loads file, glade/MyClass.glade into @builder
        set_glade_all() #populates glade controls with insance variables (i.e. Myclass.label1) 
        show_window() 
    end 

    def button1__clicked(*argv)
        if client.authorized? 
            puts "true"
        end
    end

end

And finally my window looks like this:
enter image description here

Now when I run this and click the login button, VR spits this out

C:/Users/*/visualruby/Test/bin/LoginWindow.rb:22:in `button1__clicked': undefined local variable or method `client' for #<LoginWindow:0x3f56aa8>
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:146:in `block (3 levels) in parse_signals'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `call'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `main'
     from C:/Ruby193/lib/ruby/gems/1.9.1/gems/vrlib-0.0.33/lib/GladeGUI.rb:331:in `show_window'
     from C:/Users/*/visualruby/Test/bin/LoginWindow.rb:17:in `show'
     from main.rb:14:in `<main>'

I don't think I'm supposed to put the client = Twitter.... stuff inside the LoginWindow class but I can't think of anywhere else to put it. Any ideas on how to fix this issue?

4

1 回答 1

2

这是您需要的快速解决方案。在你的 LoginWindow.rb

def initialize
   @client = TwitterOAuth::Client.new( 
       :consumer_key => '****',
       :consumer_secret => '****',
       :token => '****-****',
       :secret => '****'
   )
end


def button1__clicked(*argv)
    if @client.authorized? 
        puts "true"
    end
end

这个解决方案的问题是现在你不能在没有初始化 LogginWindow 的情况下调用 button1_clicked,所以要小心。

于 2013-05-01T05:42:43.900 回答