To was taken out of the compiler in version 2.4 and put into a half ass broken gem. I’m still trying to figure out how to get it working so I can patch it.
If you want to learn Tk you need Ruby v2.3 because it’s still built with the interpreter.
However, I did knowtice your code. To has been streamed lined. Once you get the version fixed your code should look like this
require ‘tk’
root = TkRoot.new {title “my title” }
You can even pass code like so
root = TkRoot.new {title “#{Dir.pwd}”}
Basic of basics. Your backbone should look like.
require ‘tk’
my_progam = TkRoot.new {title “my program”}
my_program[“geometry”] = ‘200x400’
my_label = TkLabel.new {text “This is my window. How cool!” ; pack}
my_button =TkButton.new {text " push me to exit" ; command {proc exit} ; pack}
I’m back to let you know I got TK working for your ruby version! if you are using linux, I made a easy patch script that takes care of the terminal work. I found the info on stack overflow and put it together to help beginners get going.
Get rid of the () in the code.
The method is
TkRoot.new{ 'title ‘something’ }#double quotes can be used too.
The difference is I. The ’ and ".
If you wanted to embed code into a string the double quotes run the code, white the single quotes will write it all I to the string.
Your variable calls the method into action.
File_path_title = Tkroot.new {title “#{Dir.pwd}”}
This string tells the TkRoot method and display the present working directory by running the code inside it.
my_program_title = TkRoot.new{title “My Program”}
This string tells the method to deploy the text in the string inside the TkRoot{. } Block.
TkRoot is the body code. It tells the interpreter to build a GUI window.
my_program[‘geometry’] = “x*y”
Calls the geometry to set the window a specific size. We call calling our variable our method is attached and gave it form.
Sorry if my question doesn’t make the intended sense. But here’s the answer.
The blackmagic lies in instance_eval() inside the initialize method:
#!/usr/bin/ruby
class Dog
def bark(a)
a
end
def initialize(&block)
instance_eval &block # or itself / self .instance_eval(&block)
end
end
Dog.new { p bark('This is a bad practice though') } # => "This is a bad practice though"
A weird syntax that all of my codes look like (and I prefer this):
class Dog
define_method(:bark) do |a| a end
define_method(:initialize) do |&block| send(:instance_eval, &block) end
end
Dog.new { p bark 'hi' } # => "hi"
Speaking of Tk, when you call instance_eval(&block), it is evaluated. The method title is defined inside TkRoot class. So it calls the method with whatever argument you pass, then the title of the window is set. Also, title returns the itself.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.