Ruby にも Method や Proc に自らのソースを吐かせる機能が組み込まれるらしい


1.9.3 への導入を目指す予定とか。こうした機能は、RubySmalltalk ばりのクラスブラウザのようなツールを作るのにも役立ちそうですね。

例:

 proc = ->(x, y) {
   x + y
 }
 proc.to_source #=> "x + y"
[ruby-dev:39301] [Feature #2080] Proc#to_source, Method#to_source


ちなみに、Squeak Smalltalk の似たような機能だと #decompileString とか #getSource が近いでしょうか。

 | block |
 block := [:x :y |
   x + y
 ].
 block decompileString   "=> '[:x :y | x + y]' "
(Integer >> #factorial) getSource asString

"=>  
'factorial
	"Answer the factorial of the receiver."

	self = 0 ifTrue: [^ 1].
	self > 0 ifTrue: [^ self * (self - 1) factorial].
	self error: ''Not valid for negative integers'''
"