Ruby + Tk's canvas and shapes are bugging out - ruby

I'm running Windows + Ruby2.3 and was messing with the tk library. I'm trying to get it to draw a grid of rectangles of different colors, but whenever I try to add a shape to the canvas my script crashes. Here's a stripped down version of the code:
require 'tk'
require 'tkextlib/tile'
root = TkRoot.new
content = Tk::Tile::Frame.new(root)
canvas = TkCanvas.new(content)
line = TkcLine.new( canvas, 0, 0, 10, 10, :fill => 'red' )
Tk.mainloop
However, when I run it I get the following error + backtrace:
C:/Ruby23/lib/ruby/2.3.0/tk/itemconfig.rb:115:in `hash_kv': wrong argument type nil (expected Array) (TypeError)
from C:/Ruby23/lib/ruby/2.3.0/tk/itemconfig.rb:115:in `itemconfig_hash_kv'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:722:in `_parse_create_args'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:735:in `create'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:758:in `create_self'
from C:/Ruby23/lib/ruby/2.3.0/tk/canvas.rb:751:in `initialize'
from C:/nopathforyou.rb:9:in `new'
from C:/nopathforyou.rb:9:in `<main>'
Anyone know what to do about this? Thanks in advance.

I've experienced the same bug, and I finally solve this issue by adding the code below:
module TkItemConfigOptkeys
def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])
hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
end
end
It should be after the 'require' statements, say, your code should be like:
require 'tk'
require 'tkextlib/tile'
module TkItemConfigOptkeys
def itemconfig_hash_kv(id, keys, enc_mode = [], conf = [])
hash_kv(__conv_item_keyonly_opts(id, keys), enc_mode, conf)
end
end
root = TkRoot.new
content = Tk::Tile::Frame.new(root)
canvas = TkCanvas.new(content)
line = TkcLine.new( canvas, 0, 0, 10, 10, :fill => 'red' )
Tk.mainloop
After adding that, my code works like a charm.

Ruby/Tk has fixed this bug in this commit.
So you can just update your tk library, as an alternative.

Related

`no implicit conversion of nil into String` wih ruby2d

So for practice with ruby, I am creating a library on top of ruby2d, but one problem.
I keep getting an error, I think setting up a window, here is the full error:
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:476:in `exist?': no implicit conversion of nil into String (TypeError)
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:476:in `add_controller_mappings'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:629:in `ext_show'
from C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/ruby2d-0.11.3/lib/ruby2d/window.rb:629:in `show'
from C:/Users/Admin/Desktop/RubyProjects/RSGE/rsge_window.rb:19:in `mainloop'
from c:/Users/Admin/Desktop/RubyProjects/test.rb:4:in `<main>'
it shows that something is wrong in ruby2d (a graphics library for ruby)
here is the code for my test file that uses my library
require './RSGE/rsge'
root = Window.new(800, 800)
root.mainloop
and here is my window class:
require 'ruby2d'
require_relative 'rsge_colors'
class Window
def initialize (w, h)
#width = w.to_i
#height = h.to_i
#title = 'Blank Window'
#background = $WHITE
end
def config
set width: #width, height: #height
set background: #background
set title: #title
end
def mainloop
show
end
end
if your wondering what rsge_colors is, here it is
$RED = 'red'
$GREEN = 'green'
$BLUE = 'blue'
$ORANGE = 'orange'
$PURPLE = 'purple'
$YELLOW = 'yellow'
$WHITE = 'white'
$BLACK = 'black'
Why am I getting this error, and how can I fix it?
Thank you for any help you can give me!
Defining class Window looks like you're overriding Ruby2D::Window class.
I've never used this library, but looking the code for some of the games in the showcase, and also the "Get started" section, it looks like the dsl must be used in the root level of your ruby file.
In your case, it should be something like:
require 'ruby2d'
require_relative 'rsge_colors'
set width: 800, height: 800
set background: $WHITE
set title: 'Blank Window'
show
Probably there must be a way to encapsulate the code into classes or modules, but if you're just learning to use the gem, probably would be better to start with the basic stuff then move to refactor these type of things.

Program not running

I have been trying to execute the following code using the command prompt. However, when I try to run it I get the following
C:>circle
C:>
Basically, nothing happens. However, I cannot figure out why this is happening as I am still new to Gosu and Ruby in general. Any help would be much appreciated.
require "rubygems"
require "gosu"
class Circle
attr_reader :columns, :rows
def initialize(radius)
#columns = #rows = radius * 2
img = Gosu::Image.new(Circle.new(50))
img.draw(200, 200, ZOrder::TOP, 0.5, 1.0, Gosu::Color::RED)
clear, solid = 0x00.chr, 0xff.chr
lower_half = (0...radius).map do |y|
x = Math.sqrt(radius ** 2 - y ** 2).round
right_half = "#{solid * x}#{clear * (radius - x)}"
right_half.reverse + right_half
end.join
alpha_channel = lower_half.reverse + lower_half
# Expand alpha bytes into RGBA color values.
#blob = alpha_channel.gsub(/./) { |alpha| solid * 3 + alpha }
end
def to_blob
#blob
end
end
There are 2 problems:
You are running file with class definition. What you need to run if you want to look at that blob is is Circle.new(10).to_blob - where 10 is radius. You can place it in separate file and require class.rb.
initialize method will be run during Circle.new()call. And you have Circle.new called in your class:
img = Gosu::Image.new(Circle.new(50))
So it will lead to infinite recursion and will cause SystemStackError. AFAIK, there is no built-in empty image constructor in gosu (maybe I am wrong).

ruby and Gtk::FontChooserDialog.new font size

I am using ruby 2.6.3, installed by compiling the source.
When using Gtk::FontChooserDialog.new, the default font size given is 10.
Is it possible to call Gtk::FontChooserDialog.new with a different size, such
as 24, so that I can avoid having to change the size each time I select a font.
Here is how I am doing things:
dialog = Gtk::FontChooserDialog.new(:title => "Select font",
:parent => self,
:action => Gtk::FileChooserAction::OPEN,
:buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
I have tried (in the argument list) :size => 24, :default_size => 24, etc. This does not work. I'm just guessing here. I have searched a lot, with no luck. I also looked in the gem sample dirs at test-gtk-font-chooser-dialog.rb and other files but no luck.
I am using Linux Mint Mate 19.1, installed a couple of weeks ago.
You need to set the size through a Pango.FontDescription. A short example in Python would be:
font_chooser = Gtk.FontChooserDialog.new(title = "Select font", parent = self)
font_description = Pango.FontDescription.new()
font_description.set_size(24 * Pango.SCALE)
font_chooser.set_font_desc(font_description)
EDIT
And here is a complete example in Ruby:
#!/usr/bin/ruby
'''
ZetCode Ruby GTK tutorial
This program runs a font dialog with a default (settable) font size.
Author: Jan Bodnar
Website: www.zetcode.com
Last modified: May 2014
'''
require 'gtk3'
require 'pango'
class RubyApp < Gtk::Window
def initialize
super
init_ui
end
def init_ui
set_title "Center"
signal_connect "destroy" do
Gtk.main_quit
end
button = Gtk::Button.new
button.set_label "Font"
button.signal_connect "clicked" do
dialog = Gtk::FontChooserDialog.new(:title => "Select font", :parent => self, :action => Gtk::FileChooserAction::OPEN, :buttons => [[Gtk::Stock::OPEN, Gtk::ResponseType::ACCEPT], [Gtk::Stock::CANCEL, Gtk::ResponseType::CANCEL]])
font_description = Pango::FontDescription.new
font_description.set_size 24 * Pango::SCALE
dialog.set_font_desc font_description
dialog.run
end
add button
set_default_size 300, 200
set_window_position Gtk::WindowPosition::CENTER
show_all
end
end
window = RubyApp.new
Gtk.main

Cannot print a text with Gosu in Ruby

Im sitting here with what I think is a very simple bug I just can not figure out.. Im trying to learn how to make games with Gosu gem with Ruby, but have hit a speed bump. Here is my code.
require "gosu"
class Hello < Gosu::Window
def initialize width = 800, height = 600, fullscreen = false
super
self.caption = "Ruby Practise"
#image = Gosu::Image.from_text self. "My text to print".
Gosu.default_font_name.
100
end
def button_down id
close if id == Gosu::KbEscape
end
def update
end
def draw
#image.draw 0, 0, 0
end
end
Hello.new.show
There is something wrong but I do not know what. I have spent at least 1 hour on it.. It complains on the String, here is the output from terminal.
hello.rb:8: syntax error, unexpected tSTRING_BEG
#image = Gosu::Image.from_text self. "My text to print".
^
hello.rb:10: syntax error, unexpected tINTEGER
I do not what I am doing wrong, do some one know? It is probably something really simple..
Use commas to separate function arguments, not dots:
#image = Gosu::Image.from_text self, "My text to print",
Gosu.default_font_name,
100

JRuby require error on .so file

I am working on a feature for an application that has to run on JRuby. Out of habit I develop on 'Native' Ruby. When I try to run any methods from this class in JRuby I get error messages like:
MissingSourceFile: no such file to load -- serialport
I think the problem lies in in the require 'serialport.so'. Is there a way to make this work in JRuby? Is there another way to require a .so file? Or a gem that can work with JRuby to provide the SerialPort class? I tried installing the ruby-serialport gem but that doesn't seem to install properly (Windows nmake issues).
Here is what my class file looks like:
require 'serialport.so'
class AlphaDisplay
#STATES
SOLID = "b"
ROTATE = "a"
BLINK = "c"
#COLORS
RED = "1"
GREEN = "2"
YELLOW = "3"
ORANGE = "7"
def self.message(address = 00, text = "ICS", color = AlphaDisplay::GREEN, state = AlphaDisplay::SOLID)
address = address.to_s
if address.length == 1
address = "0#{address}"
end
string = 1.chr + 90.chr + address + 2.chr + 65.chr + 65.chr + 27.chr + 26.chr + state + 28.chr + color + text + 4.chr
return string
end
def self.test(address = 00, text = "ICS", color = AlphaDisplay::GREEN, state = AlphaDisplay::SOLID)
sp = SerialPort.new(0, 9600, 8, 1, SerialPort::NONE)
sp.write(message(address,text,color,state))
sp.close
end
end
def SerialPort::new(port, *params)
sp = create(port)
begin
sp.set_modem_params(*params)
rescue
sp.close
raise
end
return sp
end
def SerialPort::open(port, *params)
sp = create(port)
begin
sp.set_modem_params(*params)
if (block_given?)
yield sp
sp.close
return nil
end
rescue
sp.close
raise
end
return sp
end
As far as I know, JRuby doesn't provide any native emulation layer, just the Ruby interpreter in Java. The problem is that ".so" files are UNIX specific and cannot run on Windows without some sort of emulation/translation.
You're better off avoiding Ruby libraries which require native extensions if you're going to use JRuby.

Resources