perl6 モップでネズミで調べる

perl6レッスン22日目
http://perl6advent.wordpress.com/2010/12/22/day-22-the-meta-object-protocol/

ネズミ=Rat=有理数クラス
モップ=MOP=Meta Object Protocol クラスやインスタンスの属性をダイナミックに発見し操作できる機能
MOPで有理数クラスを調べてみる。


> Rat
Rat()

属性は?


> Rat.^attributes
$!numerator $!denominator
> my $r=Rat.new(2,3);
0.666666666666667
> $r.numerator
2
> $r.denominator
3

メソッド


> Rat.^methods
new nude perl Bridge Bool Rat Num succ pred ACCEPTS Real Int Complex...

沢山ある。


> Rat.^methods.elems
200

ローカルってのはそのクラスで定義されたって意味か?


> Rat.^methods(:local)
new nude perl Bridge Bool Rat Num succ pred ACCEPTS Real Int Complex Str reals isNaN abs exp ln sqrt roots sign floor ceiling truncate round cis unpolar rand sin asin cos acos tan atan sec asec cosec acosec cotan acotan sinh asinh cosh acosh tanh atanh sech asech cosech acosech cotanh acotanh atan2 Numeric log log10 to-radians from-radians numerator denominator

pickってのはリストからランダムに選んでくれる


> Rat.^methods(:local).pick(5)
sech floor truncate abs acosec
> Rat.^methods(:local).pick(5)
acosh cis floor atan2 cosech
> Rat.^methods(:local).pick(5)
denominator Complex Real log10 cotan

ARRAY.grep正規表現を使うにはどうすればいいのだろうか?


> Rat.^methods(:local).grep('log')
log
> Rat.^methods(:local).grep('tan')
tan
> Rat.^methods(:local).grep('.*tan.*')

> Rat.^methods(:local).grep(/tan/)
===SORRY!===
Method 'match' not found for invocant of class 'Perl6MultiSub'

メソッドのシグニチャを見る


> Rat.^methods(:local).grep('log')[0].signature
Signature()<0x77c4990>
> Rat.^methods(:local).grep('log')[0].signature.perl
:(Numeric $x: Numeric $base = { ... };; *%_)
> $r.log()
-0.405465108108164
$r.log($r)
1