J
Joseph D.
While working on the end assignment of a tutorial I encountered a
problem that I have found multiple solutions to, but can not understand
why it will not work the way I initially tried to code it. The program
(3 instances of it, working method 1, working method 2, and non-working)
are attached.
this code had the same behavior in the following enviroments:
ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-linux]
Tk::TK_PATCHLEVEL
=> "8.5.9"
snipits of code in question:
non-working:
@button = Tk::Tile::Label.new($content){image @image}.gridcolumn =>
@position, :row => 1, :sticky =>'we')
working method 1:
@button = Tk::Tile::Label.new($content).gridcolumn => @position, :row
=> 1, :sticky =>'we')
@button['image'] = @image
working method 2:
@button = Tk::Tile::Label.new($content){image @@image}.gridcolumn =>
@position, :row => 1, :sticky =>'we')
The non-working method with @image as an instance variable of the class
gave no error to traceback, but did not work.
Using a class variable of the class, method 2, worked, but seemed like a
poor coding practice to me. (I am trying to learn good ways not bad
ones)
assigning @image to the label after the Label.new method was complete
rather than during the execution of the .new method works as well. Again
this method seems to me to be a bad programing practice.
If anyone could explain to me how the scope for ruby::tk interface works
for this method, and why or how to fix this properly I would greatly
appreciate it.
Attachments:
http://www.ruby-forum.com/attachment/5630/testshowingdice.rb
http://www.ruby-forum.com/attachment/5631/testshowingdice2.rb
http://www.ruby-forum.com/attachment/5632/testshowingdice3.rb
problem that I have found multiple solutions to, but can not understand
why it will not work the way I initially tried to code it. The program
(3 instances of it, working method 1, working method 2, and non-working)
are attached.
this code had the same behavior in the following enviroments:
ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]
ruby 1.8.7 (2010-12-23 patchlevel 330) [i686-linux]
Tk::TK_PATCHLEVEL
=> "8.5.9"
snipits of code in question:
non-working:
@button = Tk::Tile::Label.new($content){image @image}.gridcolumn =>
@position, :row => 1, :sticky =>'we')
working method 1:
@button = Tk::Tile::Label.new($content).gridcolumn => @position, :row
=> 1, :sticky =>'we')
@button['image'] = @image
working method 2:
@button = Tk::Tile::Label.new($content){image @@image}.gridcolumn =>
@position, :row => 1, :sticky =>'we')
The non-working method with @image as an instance variable of the class
gave no error to traceback, but did not work.
Using a class variable of the class, method 2, worked, but seemed like a
poor coding practice to me. (I am trying to learn good ways not bad
ones)
assigning @image to the label after the Label.new method was complete
rather than during the execution of the .new method works as well. Again
this method seems to me to be a bad programing practice.
If anyone could explain to me how the scope for ruby::tk interface works
for this method, and why or how to fix this properly I would greatly
appreciate it.
Attachments:
http://www.ruby-forum.com/attachment/5630/testshowingdice.rb
http://www.ruby-forum.com/attachment/5631/testshowingdice2.rb
http://www.ruby-forum.com/attachment/5632/testshowingdice3.rb