[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Defining a Method

Dave Mccormick

9/17/2007 9:46:00 PM

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
--
Posted via http://www.ruby-....

2 Answers

Rick DeNatale

9/17/2007 10:07:00 PM

0

On 9/17/07, Dave Mccormick <mac@mackrackit.com> wrote:
> 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

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.denh...

Dave Mccormick

9/17/2007 10:23:00 PM

0

Rick Denatale wrote:
> Try moving this until someplace after you initialize the label variable
> --
> Rick DeNatale
>
> My blog on Ruby
> http://talklikeaduck.denh...

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?
--
Posted via http://www.ruby-....