# "Programming Ruby", ed. 2, P747. Script generated different

**URL:** <https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539>\
**Category:** Ruby\
**Created:** [January 15, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539 "2006-01-15T16:10:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 15, 2006, 4:10pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/1 "2006-01-15T16:10:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 15, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/2 "2006-01-15T17:23:59Z")

</div>

On Sun, 15 Jan 2006 16:08:15 +0100, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> new_x = 5*scale*sin(i) + 200 + scale*sin(i*2)
> points[0],[0], points[0],[1],
> 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

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

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

I’ll go diff the two codes now.

David V.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 15, 2006, 5:34pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/3 "2006-01-15T17:34:33Z")

</div>

On Sun, 15 Jan 2006 17:22:17 +0100, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > #!/usr/bin/ruby  
> > points = []  
> > col = sprintf(“#%02x%02x%02x”, r.to\_i, g.to\_i, b.to\_i)  
> > end
> 
> David V.

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

2a3

> 

## 23,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 V.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 15, 2006, 6:22pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/4 "2006-01-15T18:22:50Z")

</div>

On Sun, 15 Jan 2006 18:02:20 +0100, Hidetoshi NAGAI  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > ```
> > > > 'capstyle' => 'round')
> > > > 
> > > > ```
> 
> :capstyle=\>‘round’)
> 
> ## (3) TkcLine.new(canvas, points, :smooth=\>‘on’, :width =\>7, :fill=\>col, :capstyle=\>‘round’)

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.

David V.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 15, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/5 "2006-01-15T18:04:37Z")

</div>

From: “David V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: “Programming Ruby”, ed. 2, P747. Script generated different  
image to book’s example  
Date: Mon, 16 Jan 2006 01:32:31 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > #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

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')
> > > 
> > > ```

> > 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’)

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 17, 2006, 2:16am UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/6 "2006-01-17T02:16:23Z")

</div>

On Jan 15, 2006, at 11:21 AM, David V. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/7 "2006-01-21T15:40:38Z")

</div>

On Jan 16, 2006, at 2:58 PM, David V. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 21, 2006, 3:40pm UTC](https://www.ruby-forum.com/t/programming-ruby-ed-2-p747-script-generated-different/61539/8 "2006-01-21T15:40:30Z")

</div>

On Mon, 16 Jan 2006 21:29:55 +0100, Dave T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Cheers
> 
> Dave

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.

I suspect John McLean has a printed version of the book and made a  
blooper  
typing it into the computer.

David V.
