On Jun 23, 11:28 pm, Ruby Q. [email protected] wrote:
The three rules of Ruby Q.:
Here is my sollution, using Tk. It is not very fancy, as uses very
basic GUI, but is what I had time for. I will probably make it fancy,
I hope I can get it on time. If someone wants to add some eye’candy,
go on (as long as it credits the original). After all, it’s for a good
cause for Ruby ;D.
#! /usr/bin/ruby
Ruby Q. 129 - Name Picker
require ‘csv’
def read_names_csv( filename )
table = CSV::parse(File.read(filename)) rescue table =
CSV::parse( filename )
titles = table.shift
return titles, table
end
def get_name( data_row )
Override if the format is different.
data_row[0…1].join(’ ')
end
class TkRoulette
require ‘tk’
attr_accessor :prizes
attr_accessor :contestants
COLORS = [:blue, :red, :green, :orange, :yellow, :pink]
def initialize( contestants = [], prizes = nil )
@contestants = contestants
@prizes = prizes
@winners = []
initialize_gui
Tk.mainloop
end
def initialize_gui
@root = TkRoot.new {
title “Ruby Q. 129 - Name Picker”
}
@name = TkLabel.new {
text ‘Press PLAY to start’
relief :groove
width 100
height 10
font :size => 60, :weight => :bold
pack :side => :top, :expand => true, :fill => :x
}
@play = TkButton.new {
text ‘PLAY!’
width 100
height 3
font :size => 30, :weight => :bold
pack :side => :bottom, :fill => :x
}.command method(:play).to_proc
end
def play( category = nil )
if @contestants.empty?
show_message( “No more contestants” )
return
end
@tick_time = 200
pick_winner
end
def pick_winner
if @tick_time <= 0
winner = @contestants.delete_at(rand(@contestants.size))
@winners << winner
show_name( "Winner is: " + get_name(winner) + “!!” )
return
end
Tk.after(200 - @tick_time, method(:pick_winner).to_proc )
@tick_time -= 5
show_name( get_name( @contestants[rand(@contestants.size)]) )
end
def show_name( name )
@name.fg COLORS[rand(COLORS.size)]
@name.text name
end
def show_message( message )
@name.fg :black
@name.text message
end
end
titles, data = read_names_csv( <<-CSV )
“First name”,“Last name”,“Organization”,“Mail”
“Fred”,“Flinstone”,“Slate Rock and Gravel
Company”,“[email protected]”
“Homer”,“Simpson”,“Sprinfield’s Nuclear Power Plant”,“[email protected]”
“George”,“Jetson”,“Spacely’s Space
Sprockets”,“[email protected]”
“Elmer”,“Food”,“Acme”,“[email protected]”
CSV
#read_names_csv(‘attendants.csv’)
if FILE == $0
roulette = TkRoulette.new(data)
end