Stefano Crocco
3/3/2007 9:02:00 PM
Alle sabato 3 marzo 2007, Alan Lake ha scritto:
> How does one refer to a tk special variable in Ruby? In Perl, it is
> $Tk::<variable>. What tk special variables are available in Ruby? I'm
> referring to variables like patchLevel, strictMotif, VERSION and
> version.
>
> In /usr/lib/ruby/1.8/tk.rb, I found TCL_VERSION, TCL_PATCHLEVEL,
> TCL_MAJOR_VERSION, TCL_MINOR_VERSION, TK_VERSION, TK_PATCHLEVEL,
> TK_MAJOR_VERSION, TK_MINOR_VERSION and JAPANIZED_TK. This may be what
> I'm looking for, but these are constants, not variables.
>
> When I try to refer to them (in irb), but after saying
> require 'tk'
> I have tried as many different ways as I can...
> puts tk::TK_VERSION
> puts TK_VERSION
> puts TK::TK_VERSION
I know nothing of tk, but the constants you looked for are in module Tk, not
TK and not tk (which can't be a module name since it doesn't begin with an
uppercase character). So, you should do:
require 'tk'
puts Tk::TK_VERSION
...
Looking at the names of the variables you mentioned (keeping in mind that I
don't know tk), I think there's a good reason for which they're constants:
for instance, TK_VERSION is the version of tk you have on your system, so I
can't think of a reason to change them.
By the way, to get the names of the constants (including classes and modules)
defined in a class or module, you can use the constants method of the Module
class. If you call it as a class method of the module class (i.e
Module.constants), it will return an array with the top-level constants. If
you call it as an instance method of a class / module it will return the
names of the constants defined in it. In your case:
require 'tk'
Module.constants.sort
=> ["ARGF", "ARGV", "ArgumentError", "Array", ..., "Tk", "TkAfter", ...]
This tells you the module you needed is called Tk. To know the constants
defined in the Tk module, you can do:
Tk.constants.sort
=> ["AUTO_PATH", "BinaryString", ..., "TK_PATCHLEVEL", "TK_VERSION", ...]
I hope this helps
Stefano