Tk configuration issue


(Phlip) #1

Newsgroupies:

This works:

    color = @myColor

    line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) {
        fill color
        }

…and this does not:

    line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) {
        fill @myColor
        }

Probably a FAQ or a not uncommon question…

…but is the stuff inside the {} working with a ‘self’ bound to the new
TkcLine object?

···


Phlip
http://www.greencheese.org/MayorZogg
– It’s a small Web, after all… –


(Dave Thomas) #2

Phlip phlip_cpp@yahoo.com writes:

…but is the stuff inside the {} working with a ‘self’ bound to the new
TkcLine object?

Yes. There’s a three sentence warning about it here (in Setting
Widget Options)

http://www.rubycentral.com/book/ext_tk.html

and a description of how it’s done here:

http://www.pragmaticprogrammer.com/ruby/articles/insteval.html

Cheers

Dave


(Mike) #3

Phlip wrote:

    color = @myColor
    line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) {
        fill color
        }

Is there any work being done to bring the syntax more towards Ruby?
Something like this, perhaps?

line = TckLine.new(canvas, x0, y0, x1, y1).fill(color).width(3)

Just a thought. Seems like it would be more Ruby-like.

···


Mike Hall


(Harry Ohlsen) #4

In an example such as Phlip gave, it would be nice to be able to pass a value
to the block, as in …

line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) { |color|
fill color
}

but there’s no way to specify the values to pass into such a block … at
least not that I know of.

If such a mechanism was available, then one could do something like pass in a
reference to the parent object, hence allowing access to its instance
variables

class Fred

  attr_reader :color

  def initialize(color)
     @color = color
  end

  def create_line
     line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) { |parent|
                 fill parent.color
           }
  end

end

Obviously, in the code above, the block parameter “parent” has not been given
a value, hence the block won’t achieve what we want. We need some way to
set it to “self” in this case.

Of course, coming up with a syntax for this may be pretty pointless, since the
amount of code required is probably going to be equivalent to setting some
local variables that will then be in scope for the block (as Phlip did in his
first example).

Sorry, just thinking out loud, in case it suggests an idea to someone. I’ve
been caught by this a number of times when writing Tk code.


(Phlip) #5

Mike Hall wrote:

line = TckLine.new(canvas, x0, y0, x1, y1).fill(color).width(3)

Just a thought. Seems like it would be more Ruby-like.

I vote yes. Consider the flow:

line = TckLine.new(canvas, x0, y0, x1, y1).
fill(color).
width(3)

That works without the mental or physical expense of string maps or blocks,
but looks just the same (when formatted according to “one idea per line”).

···


Phlip
http://www.c2.com/cgi/wiki?RatAndTuyen
– To catch a bug, you’v got to learn to think like a bug –


(MENON Jean-Francois) #6

Mike Hall wrote:

Phlip wrote:

    color = @myColor
    line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) {
        fill color
        }

Is there any work being done to bring the syntax more towards Ruby?
Something like this, perhaps?

    line = TckLine.new(canvas, x0, y0, x1, y1).fill(color).width(3)

Just a thought. Seems like it would be more Ruby-like.


Mike Hall

personaly I use “named parameters”:

line = TckLine.new(canvas, x0, y0, x1, y1,“fill”=> color, “width”=> 3)

all the parameters are inside the parenthesis, and it makes only one
function call.

jf

···


/ do you play Go? \

http://jeanfrancois.menon.free.fr/rubygo |
\ /


    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            >>----w |
            >>     >>

(Phlip) #7

Thanks Dave - the Bookmarks are set…

Harry Ohlsen wrote:

In an example such as Phlip gave, it would be nice to be able to pass a
value to the block, as in …

line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) { |color|
fill color
}

  other = self

line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]) { 
               fill other.getColor
         }

However, I thank those who respected my seniority but did not test my
senility by replying with the obvious:

line = TkcLine.new(aCanvas, [fromX, fromY, toX, toY]
                    'color' => getColor
                    )

I already knew that but forgot to write it as a counter-example.

Were I to care about the cost of stringing verses the cost of blocking, I
could ask if building a map of strings be more expensive. But I don’t so I
won’t.

···


Phlip
http://clublet.com/c/c/why?ZenoBuddhism
– Have a :slight_smile: day –


(Joel VanderWerf) #8

Phlip wrote:

line = TckLine.new(canvas, x0, y0, x1, y1).
fill(color).
width(3)

You can already do:

line = TckLine.new(canvas, x0, y0, x1, y1)
line.fill(color)
line.width(3)

I prefer this because if you delete the last line, you don’t cause a
syntax error (or worse).


(Phlip) #9

Joel VanderWerf wrote:

Phlip wrote:

line = TckLine.new(canvas, x0, y0, x1, y1).
fill(color).
width(3)

You can already do:

line = TckLine.new(canvas, x0, y0, x1, y1)
line.fill(color)
line.width(3)

I prefer this because if you delete the last line, you don’t cause a
syntax error (or worse).

A TkCanvas has sufficient performance issues that one hesitates to
construct one and then change its props. This may be cheap.

If it is expensive, and if we can thwart this via any more elaborate
construction, then how we construct is no longer an esthetic issue.

···


Phlip
http://www.greencheese.org/LucidScheming
– The meetings will continue
until the schedule improves –