Derek Adams Derek Adams - 4 months ago 6
Ruby Question

Is there a global_variable_set for ruby?

If one was to make a Tk GUI in ruby and make multiple check boxes with different global variable names. What would be the most efficient way of doing so?

I found the instance_variable_set, but as this wouldn't fit my scenario. I wondered if there was such thing as something like a global_variable_set.

Eg.

info = [orange,apple,banana,grape,watermelon]
$var = TkVariable.New
info.each_with_index {|inf,index|
TkCheckButton.new(frame1) do
text "#{inf}"
onvalue "#{inf}"
variable global_variable_set("$var#{index}", inf)
end
end

Answer

Well, I didn't test this. But I suppose this would be of some help to you:

As per this documentation

There seems a method defined in lib/quality_extensions/global_variable_set.rb

global_variable_set(var, value, options = {}) ⇒ Object

Sets the global variable var, which can either be a symbol or an actual global variable (use :match_object).

global_variable_set(:$a, 'new')
global_variable_set($a, 'new', :match_object => true)
global_variable_set(:$a, "StringIO.new", :eval_string => true)

Hope this helps?

Comments