From: gabriele renzi Date: 2004-06-01T17:23:42+09:00 Subject: Re: Andreas' practical language comparison il Sun, 30 May 2004 17:25:03 +0200, Hynek Schlawack ha scritto:: >* "Robert Klemme" wrote: > >>> It looks like Ruby is missing in "Andreas' practical language >>> comparison" (Here's the page >>> http://www.kochandreas.com/home/language/lang.htm) I guess it'd be >>> nice if Ruby won an appropriate place in the competition. >> Partly done. Some shorties there. Any volunteers for GUI stuff? > >I found time to do "Red Ball" just now, hope no one else was faster. I >think it looks pretty cool in ruby (dunno if it goes even >better). Especially comparing it to the python-version... I believe some little tweaks may make this little more rubyish: get rid of global variables using a constant, a local, and defining move_player as a singleton method of the canvas object #!/bin/env ruby # We use the Tk-Toolkit require 'tk' # Create a root-widget, a canvas and a hash to store the squares root = TkRoot.new { title "Red Ball" } canvas = TkCanvas.new(root) { width 320; height 200 } Fig = Hash.new # Create the squares with the appropriate colors on random places ['black', 'red'].each do | colour | x = (rand * (320 - 16)).to_i y = (rand * (200 - 16)).to_i Fig[colour] = TkcRectangle.new(canvas, x, y, x+15, y+15, 'fill' => colour) end canvas.pack # Move the square in the desired direction, exit if we have won def canvas.move_player(x, y) move(Fig['red'], x, y) exit if (coords(Fig['red']) == coords(Fig['black'])) end # Bind the actions to keys root.bind("s") { canvas.move_player(-1, 0) } root.bind("d") { canvas.move_player( 1, 0) } root.bind("e") { canvas.move_player( 0, -1) } root.bind("x") { canvas.move_player( 0, 1) } # Finally give up control to Tk Tk.mainloop