"Programming Ruby", ed. 2, P747. Script generated different imageto book's example

J

John Maclean

Chaps,

Please find below a script taken from "Programming Ruby", ed. 2, P747. Its intention is to "show off" the abilities of Tk and widgets. The script works but generates a different image to that shown in the book. What results do you guys have? I would be interested to know.

#!/usr/bin/ruby
require 'tk'
include Math

TkRoot.new do |root|
title "Curves"
geometry "400x400"
TkCanvas.new(root) do |canvas|
width 400
height 400
pack('side' => 'top' , 'fill' => 'both' , 'expand' => 'yes' )
points = [ ]
10.upto(30) do |scale|
(0.0).step(2*PI,0.1) do |i|
new_x = 5*scale*sin(i) + 200 + scale*sin(i*2)
new_y = 5*scale*cos(i) + 200 + scale*cos(i*6)
points << [ new_x, new_y ]
f = scale/5.0
r = (Math.sin(f)+1)*127.0
g = (Math.cos(2*f)+1)*127.0
b = (Math.sin(3*f)+1)*127.0

col = sprintf("#%02x%02x%02x", r.to_i, g.to_i, b.to_i)
if points.size ==3
TkcLine.new(canvas,
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],
'smooth' => 'on',
'width' => 7,
'fill' => col,
'capstyle' => 'round')
points.shift
end
end
end
end
end
Tk.mainloop

#errors generated
jayeola@tp20$ /usr/lib/ruby/1.8/tk.rb:2313: warning: redefine encoding=
/usr/lib/ruby/1.8/tk.rb:2316: warning: redefine encoding
 
D

David Vallner

Chaps,

Please find below a script taken from "Programming Ruby", ed. 2, P747. = =20
Its intention is to "show off" the abilities of Tk and widgets. The =20
script works but generates a different image to that shown in the book.= =20
What results do you guys have? I would be interested to know.

#!/usr/bin/ruby
require 'tk'
include Math

TkRoot.new do |root|
title "Curves"
geometry "400x400"
TkCanvas.new(root) do |canvas|
width 400
height 400
pack('side' =3D> 'top' , 'fill' =3D> 'both' , 'expand' =3D> 'yes' )
points =3D [ ]
10.upto(30) do |scale|
(0.0).step(2*PI,0.1) do |i|
new_x =3D 5*scale*sin(i) + 200 + scale*sin(i*2)
new_y =3D 5*scale*cos(i) + 200 + scale*cos(i*6)
points << [ new_x, new_y ]
f =3D scale/5.0
r =3D (Math.sin(f)+1)*127.0
g =3D (Math.cos(2*f)+1)*127.0
b =3D (Math.sin(3*f)+1)*127.0

col =3D sprintf("#%02x%02x%02x", r.to_i, g.to_i, b.to_i)
if points.size =3D=3D3
TkcLine.new(canvas,
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],
'smooth' =3D> 'on',
'width' =3D> 7,
'fill' =3D> col,
'capstyle' =3D> 'round')
points.shift
end
end
end
end
end
Tk.mainloop

#errors generated
jayeola@tp20$ /usr/lib/ruby/1.8/tk.rb:2313: warning: redefine encoding=3D
/usr/lib/ruby/1.8/tk.rb:2316: warning: redefine encoding


Something must have goofed up between your and my version of the book, on=
=20
whichever version of ruby and tcl/tk that comes with Cygwin updated a wee=
k =20
ago, what you posted does indeed generate something strange, however no =20
errors.

Then I pasted the code from the PDF, version 2004-9-30, page 754 in the =20
file, 726 in the book, I got the same image as in the book.

I'll go diff the two codes now.

David Vallner
 
D

David Vallner

Chaps,

Please find below a script taken from "Programming Ruby", ed. 2, P747.= =20
Its intention is to "show off" the abilities of Tk and widgets. The =20
script works but generates a different image to that shown in the book= =20
What results do you guys have? I would be interested to know.

#!/usr/bin/ruby
require 'tk'
include Math

TkRoot.new do |root|
title "Curves"
geometry "400x400"
TkCanvas.new(root) do |canvas|
width 400
height 400
pack('side' =3D> 'top' , 'fill' =3D> 'both' , 'expand' =3D> 'yes' = )
points =3D [ ]
10.upto(30) do |scale|
(0.0).step(2*PI,0.1) do |i|
new_x =3D 5*scale*sin(i) + 200 + scale*sin(i*2)
new_y =3D 5*scale*cos(i) + 200 + scale*cos(i*6)
points << [ new_x, new_y ]
f =3D scale/5.0
r =3D (Math.sin(f)+1)*127.0
g =3D (Math.cos(2*f)+1)*127.0
b =3D (Math.sin(3*f)+1)*127.0

col =3D sprintf("#%02x%02x%02x", r.to_i, g.to_i, b.to_i)
if points.size =3D=3D3
TkcLine.new(canvas,
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],
'smooth' =3D> 'on',
'width' =3D> 7,
'fill' =3D> col,
'capstyle' =3D> 'round')
points.shift
end
end
end
end
end
Tk.mainloop

#errors generated
jayeola@tp20$ /usr/lib/ruby/1.8/tk.rb:2313: warning: redefine encoding= =3D
/usr/lib/ruby/1.8/tk.rb:2316: warning: redefine encoding


Something must have goofed up between your and my version of the book, = =20
on whichever version of ruby and tcl/tk that comes with Cygwin updated = a =20
week ago, what you posted does indeed generate something strange, =20
however no errors.

Then I pasted the code from the PDF, version 2004-9-30, page 754 in the= =20
file, 726 in the book, I got the same image as in the book.

I'll go diff the two codes now.

David Vallner


Diff results after tweaking all the whitespace differences between the tw=
o =20
files (the first is the PDF version, the second the code you posted):

2a323,25c24,26
< points[0][0], points[0][1],
< points[1][0], points[1][1],
< points[2][0], points[2][1],
---
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],

*coughs*
I rest my case.

David Vallner
 
H

Hidetoshi NAGAI

From: "David Vallner" <[email protected]>
Subject: Re: "Programming Ruby", ed. 2, P747. Script generated different image to book's example
Date: Mon, 16 Jan 2006 01:32:31 +0900

Please ignore this warning. That has no problem.
TkcLine.new(canvas,
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],
'smooth' => 'on',
'width' => 7,
'fill' => col,
'capstyle' => 'round')
Diff results after tweaking all the whitespace differences between the two
files (the first is the PDF version, the second the code you posted):

2a323,25c24,26
< points[0][0], points[0][1],
< points[1][0], points[1][1],
< points[2][0], points[2][1],
---
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],

On current Ruby/Tk, a canvas Item can accept an array or arrays
as its coords.
That is, all of the followings are valid.
----------------------------------------------------------------
(0) TkcLine.new(canvas,
points[0][0], points[0][1],
points[1][0], points[1][1],
points[2][0], points[2][1],
:smooth=>'on', :width =>7, :fill=>col, :capstyle=>'round')

(1) TkcLine.new(canvas,
points[0], points[1], points[2],
:smooth=>'on', :width =>7, :fill=>col, :capstyle=>'round')

(2) TkcLine.new(canvas,
points.flatten,
:smooth=>'on', :width =>7, :fill=>col, :capstyle=>'round')

(3) TkcLine.new(canvas,
points,
:smooth=>'on', :width =>7, :fill=>col, :capstyle=>'round')
 
D

David Vallner

[snip]
TkcLine.new(canvas,
points[0],[0], points[0],[1],
points[1],[0], points[1],[1],
points[2],[0], points[2],[1],
'smooth' =3D> 'on',
'width' =3D> 7,
'fill' =3D> col,
'capstyle' =3D> 'round')

On current Ruby/Tk, a canvas Item can accept an array or arrays
as its coords.
That is, all of the followings are valid.
----------------------------------------------------------------
(0) TkcLine.new(canvas,
points[0][0], points[0][1],
points[1][0], points[1][1],
points[2][0], points[2][1],
:smooth=3D>'on', :width =3D>7, :fill=3D>col, =20
:capstyle=3D>'round')

(1) TkcLine.new(canvas,
points[0], points[1], points[2],
:smooth=3D>'on', :width =3D>7, :fill=3D>col, =20
:capstyle=3D>'round')

(2) TkcLine.new(canvas,
points.flatten,
:smooth=3D>'on', :width =3D>7, :fill=3D>col, =20
:capstyle=3D>'round')

(3) TkcLine.new(canvas,
points,
:smooth=3D>'on', :width =3D>7, :fill=3D>col, =20
:capstyle=3D>'round')
----------------------------------------------------------------


The bug was passing in literal arrays with one element, 0, or 1, to =20
TkcLine::new. Using the whole 2D array at once looks nifty, it would =20
indeed prevent that typo / slip of mind.

David Vallner
 
D

Dave Thomas

The bug was passing in literal arrays with one element, 0, or 1, to
TkcLine::new. Using the whole 2D array at once looks nifty, it
would indeed prevent that typo / slip of mind.


So, (he says, coming to the discussion late), what does this mean for
the code i the book?


Cheers


Dave
 
D

David Vallner

So, (he says, coming to the discussion late), what does this mean for =20
the code i the book?


Cheers


Dave


The code is fine, for a code example explicitness seems more important to=
=20
me instead of showing off - that's for the Ruby/Tk documentation to =20
explain.

I suspect John McLean has a printed version of the book and made a bloope=
r =20
typing it into the computer.

David Vallner
 
D

Dave Thomas

The code is fine, for a code example explicitness seems more
important to me instead of showing off - that's for the Ruby/Tk
documentation to explain.

/me sighs happily

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top