require "wxruby"
class Array
def shuffle
ret, org = Array.new, self.dup
ret<<org.delete_at(rand(org.length)) while org.length > 0
return ret
end
end
class RandomGenApp < Wx::App
def on_init
$xrc = Wx::XmlResource.get
$xrc.init_all_handlers
$xrc.load("_RandomNumber.xrc")
RandomGenDialog.new(self).show(true)
end
end
class RandomGenDialog < Wx::Dialog
def initialize(parent)
@coupons = (1..300).to_a
super()
$xrc.load_dialog_subclass(self, parent, 'ID_DIALOG')
@number = Wx::Window.find_window_by_id(Wx::xrcid('wxID_STATIC'))
@btn_select = Wx::Window.find_window_by_id(Wx::xrcid('ID_BUTTON_SELECT'))
evt_button(@btn_select.get_id) { onBtnPop }
evt_close() { Wx::get_app.exit_main_loop }
end
def onBtnPop
50.times { @number.set_label(@coupons.shuffle[0].to_s) }
@number.set_label(@coupons.shuffle.shift.to_s)
end
end
RandomGenApp.new.main_loop

작년 2학기 동아리 세미나때 Ruby 를 소개하면서 예제로 만들어봤던 프로그램. 경품 추첨을 위해 사용되었던, 배열을 섞고 첫번째 값을 뽑아내는 프로그램이다. 40줄짜리 간단한 프로그램이지만 Ruby 에 대한 설명과 wxWindows 에 대한 설명을 병행하면서 시연하다보니 예상시간을 훌쩍 넘어버렸었다. 다 짜고 나니까 청중들 반이 자고 있더라.
그리고 이번에는, 특정 대역의 번호만 줄줄이 나와버리는 기현상이 나타나서 우리 학교가 경품을 싹쓸이해가는 사태가 발생해 버렸다.-_-;