Ruby - 未知方法init

时间:2015-11-08 13:51:40

标签: ruby user-interface gtk3

我学习了一些ruby,今天我尝试用gtk3开始构建GUI。 我找到了这个教程

http://zetcode.com/gui/rubygtk/introduction/

我复制了试图在我的终端上运行脚本的代码。

sudo ruby gui.rb

但我只得到这个错误代码。

  

gui.rb:34:in <main>': undefined method init'for Gtk:Module(NoMethodError)

我已使用以下命令安装了gtk3 gem。

sudo gem install gtk3

之前我尝试将gtk用于..

sudo gem install gtk2

错误在哪里? 我已经成功安装了gtk。

3 个答案:

答案 0 :(得分:2)

您不必再使用user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_proxied any; gzip_vary on; gzip_types text/plain text/xml text/css text/javascript application/json application/javascript application/x-javascript application/ecmascript application/xml application/rss+xml application/atom+xml application/rdf+xml application/xml+rss application/xhtml+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject image/svg+xml image/x-icon application/atom_xml; gzip_buffers 16 8k; add_header X-Frame-Options SAMEORIGIN; ssl_protocols TLSv1.2 TLSv1.1 TLSv1; keepalive_timeout 60; proxy_connect_timeout 60; proxy_send_timeout 60; proxy_read_timeout 60; send_timeout 60; client_max_body_size 100M; proxy_cache_path /dev/shm levels=1:2 keys_zone=mcache:16m inactive=600s max_size=512m; proxy_cache_methods GET HEAD; proxy_cache_min_uses 1; proxy_cache_key "$request_method$host$request_uri"; proxy_cache_use_stale timeout updating; proxy_ignore_headers Cache-Control Expires Set-Cookie; server { listen 80; server_name my.company.com; return 301 https://$host$request_uri; } server { listen 443 http2 default_server ssl; server_name my.company.com; ssl_certificate /etc/nginx/ssl/cert.pem; ssl_certificate_key /etc/nginx/ssl/cert.key; ssl on; # API server location /api { if ($request_method = OPTIONS) { return 204; } # Proxy settings proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://internal-api-server; # Error handlers error_page 504 /errors/504.json; if ($request_uri ~* /api/foo/bar) { proxy_cache_bypass 0; proxy_no_cache 0; proxy_cache mcache; # mcache=RAM proxy_cache_valid 200 301 302 30m; proxy_cache_valid 403 404 5m; proxy_cache_lock on; proxy_cache_use_stale timeout updating; add_header X-Proxy-Cache $upstream_cache_status; } # CORS headers add_header 'Access-Control-Allow-Origin' '*' always; add_header 'Allow' '*' always; add_header 'Access-Control-Allow-Methods' 'GET, HEAD, POST, PUT, DELETE, PATCH, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept, Referrer, User-Agent, Authorization, X-Impersonate-As, X-Request-Context' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Expose-Headers' 'Authorization, X-Info-Resources' always; add_header 'Access-Control-Max-Age' 3628800 always; } location ^~ /errors/ { internal; root /etc/nginx/static-files/; } location /nginx_status { stub_status on; access_log off; } } } 方法了。

从下面的教程中,只需保持代码如下:

GTK.init

此外,您不应该使用sudo来安装您的宝石,因此您可以在不成为root的情况下运行脚本。 运行#!/usr/bin/ruby ''' ZetCode Ruby GTK tutorial This program centers a window on the screen. Author: Jan Bodnar Website: www.zetcode.com Last modified: May 2014 ''' require 'gtk3' class RubyApp < Gtk::Window def initialize super set_title "Center" signal_connect "destroy" do Gtk.main_quit end set_default_size 300, 200 set_window_position Gtk::Window::Position::CENTER show end end # Gtk.init window = RubyApp.new Gtk.main 会将您的宝石安装到根目录中。

答案 1 :(得分:0)

错误意味着您没有定义您尝试在程序第34行上运行的方法。

如果你想在Ruby中尝试使用GUI,我建议使用Shoes,鞋子非常简单,那里有很多很棒的教程。

这是指向鞋子的链接:http://shoesrb.com/

答案 2 :(得分:0)

我正在使用较旧的代码调试类似的问题。 gtk gem的3.0.x系列有(a)没有init方法,(b)没有我能找到的最新文档,......: - (

要返回使用该代码的gtk3版本,请尝试

gem install gtk --version "~> 2.2"
如果您使用的是

,请在您的gemspec中使用~> 2.2