Defining a Method

Hi,
I am new to Ruby from basic. I am having a hard time getting my head
around the “def”.

I have an Entry widget for a number, a label widget for the answer,
button widget to multiply entry by 2 and place in Label.

I know this should/is simple, but cannot seem to get started.

Here is the messed up code that I have, would someone tell me what to
do?

Thank you,
Dave

#!/usr/bin/env ruby
require ‘gtk2’

window =Gtk::Window.new
window.signal_connect(“delete_event”) do
Gtk.main_quit
false
end

box1 = Gtk::HBox.new(false, 0)
window.add(box1)

buttonE = Gtk::Button.new(“Exit”)
buttonE.signal_connect(“clicked”)do
Gtk.main_quit
false
end

box1.pack_start(buttonE, false, false, 10)

buttonC = Gtk::Button.new(“Click”)
buttonC.signal_connect(“clicked”) {
label.text=(entry.text.to_f * 2).to_s
}

box1.pack_start(buttonC, true, true, 10)

label = Gtk::Label.new(“LABEL”)
box1.pack_start(label, true, true, 10)

entry = Gtk::Entry.new
entry.set_max_length( 50 )
box1.pack_start(entry, true, true, 10)

window.border_width = 100
window.show_all
Gtk.main

On 9/17/07, Dave M. [email protected] wrote:

do?
Gtk.main_quit
false

window.border_width = 100
window.show_all
Gtk.main

I suspect that the first problem is that a the point you invoke:

buttonC.signal_connect(“clicked”) {
label.text=(entry.text.to_f * 2).to_s
}

the variable label has not yet been defined in the outer scope.

Try moving this until someplace after you initialize the label variable

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick Denatale wrote:

Try moving this until someplace after you initialize the label variable

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Thank you, that did it for this one. I thought something like this had
to be put in a “def” to work.

When should a method be used?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs