404 Blog Not Found:js/scheme/perl/ruby/C - この世で最も役に立たない関数
▼ブロック版
Squeak Smalltalk
| pointless | pointless := [ | sender | sender := thisContext sender. sender tempAt: (sender tempNames indexOf: #pointless) put: nil. 'turning off myself']. pointless value. "=> 'turning off myself' " pointless value. "=> nil "
とと…。これで十分でした。
Squeak Smalltalk
| pointless | pointless := [ pointless := nil. 'turning off myself']. pointless value. "=> 'turning off myself' " pointless value. "=> nil "
▼メソッド版
Squeak Smalltalk
self class compile: 'pointless self class removeSelector: #pointless. ^''turning off myself'''. self pointless. "=> 'turning off myself' " self pointless. "=> MessageNotUnderstood: UndefinedObject>>pointless "
Ruby の def 版も同じ方針でいいはずです。
Ruby
def pointless puts "turning off myself" self.class.module_eval{ remove_method(:pointless) } end pointless #=> "turning off myself" pointless #=> NameError: undefined local variable or method `pointless' for main:Object
▼スレッド版
趣を変えて、ネタ元のマシンの動作のやりきれなさをスレッド(Smalltalk ではプロセスと呼ばれる)のレジュームとサスペンドとを蓋の開閉に見立てて表現してみました。
Squeak Smalltalk
| pointless | pointless := [[ Transcript cr; show: 'switch off!'. Processor activeProcess suspend. ] repeat] newProcess. pointless priority: Processor userBackgroundPriority. World findATranscript: nil. 5 timesRepeat: [ Transcript cr; show: 'switch on...'. pointless resume. (Delay forSeconds: 2) wait]. pointless terminate
出力はこんな感じ。
switch on... switch off! switch on... switch off! switch on... switch off! switch on... switch off! switch on... switch off!
ほぼ同じものを Ruby で書いたのがこちら。
Ruby
pointless = Thread.new{ loop{ puts "switch off!" Thread.stop }} 5.times{ puts "switch on..." pointless.run sleep 2} pointless.kill
Ruby のスレッドは stop した状態で作れないみたいなので、いきなりスイッチオフ!から始まってしまうのはご愛敬。
switch off! switch on... switch off! switch on... switch off! switch on... switch off! switch on... switch off! switch on... switch off!