Ruby Tk - method title

Hi, I am new to Ruby Tk.

One thing I noticed weird is:
root = TkRoot.new { title("hi") }
[Ruby - Tk Guide]

Look at the method title, you pass title to block.
The outputs and return values of the following codes are:

p defined? title # => nil
root = TkRoot.new { p defined? title } # output: method

root = TkRoot.new { p method(:title).source_location } # => ["/home/sourav/.gem/ruby/2.6.0/gems/tk-0.2.0/lib/tk/wm.rb", 512]

How does the title get defined inside the block?

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}

Tk.mainloop

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.

run it, it will install the ruby dev and the tk gem then make a test tk window.
you can git clone it here
GitHub - CufeHaco/Tk_Patch: All In One patch script and tk gem install to get RubyTk working again for versions above Ruby 2.3

Yes, I am on Arch Linux with Ruby 2.6.3.

My question is how the method title is defined inside
TkRoot.new{ title('something') }

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"

Stackoverflow - How does title method inside TkRoot block work in Ruby?

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.