From: Hidetoshi NAGAI Date: 2005-07-15T14:46:01+09:00 Subject: Re: Perl/Tk, CheckButtons, and modifying variables From: "David Douthitt" Subject: Re: Perl/Tk, CheckButtons, and modifying variables Date: Fri, 15 Jul 2005 08:15:51 +0900 Message-ID: <1121382859.564956.239480@g44g2000cwa.googlegroups.com> > I didn't see anything in "The Ruby Way" and I didn't have my PickAxe > book with me. The PerlTk documents don't have a "Variable" or a > "TkVariable" listed anywhere. Ruby/Tk accesses to Tcl's variables through TkVariable objects. # Of course, "wait" and "trace" are available like as a Tcl's variable. As you know, each value is kept as a string on a Tcl/Tk interpreter. So, a Ruby's object which set to a Tcl's variable is converted to a string (with TkUtil._get_eval_string() and so on). Usually, use TkVariable#value/value= when the Tcl's variable is a scalar type, or use TkVariable#[]/[]= when the variable is an array. TkVariable.new creates a scalar variable. However, TkVariable.new() or TkVariable.new_hash creates an array variable. For example, ----------------------------------- v = TkVariable.new v['a'] = 1 #=> raise exception!! ----------------------------------- ----------------------------------- v = TkVariable.new(Hash.new) v['a'] = 1 v['a'] #=> "1" v.value #=> {"a"=>"1"} ----------------------------------- The following is based on the latest version of Ruby/Tk. To help to access the value of Tcl's variable, TkVariable has some kind of access methods. ------------------------------------------------- -----------+-----------+----------- type | get | set ===========+===========+=========== String | value | value= -----------+-----------+----------- Symbol | symbol | symbol= -----------+-----------+----------- Numeric | numeric | numeric= -----------+-----------+----------- Boolean | bool | bool= -----------+-----------+----------- List | list | list= -----------+-----------+----------- Num-List | numlist | numlist= -----------+-----------+----------- Variable | variable | variable= -----------+-----------+----------- Widget | window | window= -----------+-----------+----------- Procedure | procedure | procedure= -----------+-----------+----------- * 'List' means that is a list of strings, and 'Num-List' means that is a list of Numeric values. * 'Variable' means that its value is a Tcl's variable name. TkVariable#variable returns a TkVariable object. * 'Widget' means that its value is a widget path. TkVariable#window returns a widget object. * 'Procedure' means that its value is a script. If set a proc object to the variable, TkVariable#procedure returns a proc object. ------------------------------------------------- Based on that, ----------------------------------- v = TkVariable.new('no') v.value #=> 'no' v.bool #=> false ----------------------------------- ----------------------------------- v = TkVariable.new(1) v.numeric += 2 v.value #=> "3" ----------------------------------- TkVariable object can have 'TYPE' of its value. Default TYPE is nil. Then, TkVariable#value returns a string. ----------------------------------- v = TkVariable.new(1) v.default_value_type #=> nil v.value #=> "1" ----------------------------------- When set the TYPE, TkVariable#value returns a value of the TYPE. ----------------------------------- v = TkVariable.new(1) v.default_value_type = :numeric v.value #=> 1 ----------------------------------- TkVariable has some operator methods. For example, ----------------------------------- v = TkVariable.new(1) v + 2 #=> 3 ----------------------------------- ----------------------------------- v = TkVariable.new('asdf') v + 'qwer' #=> 'asdfqwer' ----------------------------------- Please see tk/variable.rb for other methods and ask if you have some questions. -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)