Why the scope is different?

Hi all,

I create a checkbutton then bind it to an event as following:

checkbutton1.bind (“ButtonPress-1”) do
puts “check”
$check=“user checks it”
end

puts $check=“user checks it”

I can see Ruby prints out “check” only but not “user checks it”.
And globale variable $check is ignored. Why is it? How can I make
global $check available outside this block?

Thanks,

It should appear. That’s really your code? The only thing that I can
think is that the last puts() is not running. Check it out.

Obviously the puts() is out of the block of what “ButtonPress-1” trigger
when you press it.

But why? I ask Ruby to puts () everything inside the block when I
press the checkbutton. I understand that if I press checkbutton or
check it, what inside the block should be executed, right? since Ruby
can puts() everything inside the block and I pass one string to a global
variable, I expect the global variable should be valid all the time.

If this is not the case, how can I make it work so that when I press or
check the checkbutton, I can puts() outside the block?

Thanks,

No, as I see in your code, there’s no #puts() before the global
variable.
You did this:

checkbutton1.bind (“ButtonPress-1”) do
puts “check”
$check=“user checks it”
end

Perhaps what you want to do this:

checkbutton1.bind (“ButtonPress-1”) do
puts “check”
puts($check = “user checks it”)
end

Then when you press the button you will receive an output like:

#=> check
#=> user checks it

Yes. it is part of my code.

And yes again the last(second puts) is not running. I want to know how
come it doesn’t run as I already press the checkbutton and see Ruby
prints out “check”.

The one outside the block is executed when you shoot your script, since
it’s in the flow of definition of a class(if it’s outside a method
definition), perhaps is that.

Hi Damián,

Please check one of my posts, entitled “how to catch the indicator for a
checkbutton” about this topic.

Hi,
there are 3 puts() here:

checkbutton1.bind (“ButtonPress-1”) do
puts “check”
puts($check = “user checks it”)
end

puts($check = “user checks it”)

I can see the results of two puts(). How about the last puts()? It is
never seen by Ruby. If global varible $check is executed in the block,
how come the one outside the block is ignored? This is the point I don’t
get it.

Thanks,

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